Wednesday, October 22, 2014

Load Here map image, using REST API

This example show how to load Here map image, using REST API (not Native SDK). HERE, a Nokia company, is a global leader in the mapping and location intelligence business.



The Here Map Image API Developer's Quick Start Guide provide information to help you start using the Map Image API.

app_id and app_code are authentication credentials. You can use the demo credentials in the examples for testing, but must substitute them with your own unique values in your website or application. See Acquiring Credentials for more information.

MainActivity.java
package com.example.androidhererestmapimage;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.support.v7.app.ActionBarActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
 
 public class LoadHereMapTask extends AsyncTask<URL, Void, Bitmap> {
  
  ImageView imageView;
  
  LoadHereMapTask(ImageView v){
   imageView = v;
  }

  @Override
  protected Bitmap doInBackground(URL... params) {
   Bitmap bm = null;
   URL urlMapImage = params[0];
   try {
    bm = BitmapFactory.decodeStream(urlMapImage.openConnection().getInputStream());
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return bm;
  }

  @Override
  protected void onPostExecute(Bitmap result) {
   imageView.setImageBitmap(result);
  }

 }

 ImageView mapImage;
 LoadHereMapTask loadHereMapTask;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mapImage = (ImageView)findViewById(R.id.mapimage);
  
  loadHereMapTask = new LoadHereMapTask(mapImage);
  
  URL urlTarget;
  try {
   urlTarget = new URL(genHereMapImageRequest());
   loadHereMapTask.execute(urlTarget);
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
  
 }
 
 private String genHereMapImageRequest(){
  
  String BaseURL = "http://image.maps.cit.api.here.com";
  String Path = "/mia/1.6/";
  String Resource = "mapview";
  String ApplicationId = "DemoAppId01082013GAL";  //for demo
  String ApplicationCode = "AJKnXv84fjrb0KIHawS0Tg"; //for demo
  String location = "52.378,13.520";     //Berlin  
  
  String rqs = BaseURL + Path + Resource 
    + "?app_id=" + ApplicationId
    + "&app_code=" + ApplicationCode
    + "&c=" + location;

  return rqs;
 }

}

/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidhererestmapimage.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />
    <ImageView
        android:id="@+id/mapimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.


download filesDownload the files.