Monday, October 27, 2014

Example of using Here Map Tile REST API on Android

HERE Map Tile API is a RESTful API that retrieves map images for all regions of the world. To get a map image, formulate a request that combines the URL and a set of parameters to specify details such as position, format, zoom level, map type of the map image. You can embed the resulting map image in web pages and applications.

The Map Tile API serves map tiles obtained by mapping points on the surface of a sphere (the globe) to points on a plane, using the normalized Mercator projection.

It's a example of using HERE Map Tile REST API on Android (not Native SDK).


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;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

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);
  }

 }
 
 SeekBar sbZoom;
 TextView textviewMapRqs512, textviewMapRqs256;
 ImageView mapImage1, mapImage2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textviewMapRqs512 = (TextView)findViewById(R.id.maprqs512);
  textviewMapRqs256 = (TextView)findViewById(R.id.maprqs256);
  mapImage1 = (ImageView)findViewById(R.id.mapimage1);
  mapImage2 = (ImageView)findViewById(R.id.mapimage2);
  sbZoom = (SeekBar)findViewById(R.id.sbzoom);
  sbZoom.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

   @Override
   public void onProgressChanged(SeekBar seekBar, int progress,
     boolean fromUser) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    
   }

   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
    updateHereMap();
   }});
  
  updateHereMap();

 }
 
 private void updateHereMap(){
  URL urlTarget;
  
  try {
   String strTarget512 = genHereMapTileRequest("/512");
   textviewMapRqs512.setText(strTarget512);
   urlTarget = new URL(strTarget512);
   new LoadHereMapTask(mapImage1).execute(urlTarget);
   
   String strTarget256 = genHereMapTileRequest("/256");
   textviewMapRqs256.setText(strTarget256);
   urlTarget = new URL(strTarget256);
   new LoadHereMapTask(mapImage2).execute(urlTarget);
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
 }
 
 private String getMercatorProjection(double lat, double lon, int z){
  /*
   * reference:
   * http://developer.here.com/rest-apis/documentation/enterprise-map-tile/topics/key-concepts.html
   */
  
  double latRad = lat * Math.PI/180;
  double n = Math.pow(2, z);
  double xTile = n * ((lon + 180)/360);
  double yTile = n * (1-(Math.log(Math.tan(latRad) + 1/Math.cos(latRad))/Math.PI))/2;
  
  String strProjection = "/"+ String.valueOf(z)
   + "/" + String.valueOf((int)xTile)
   + "/" + String.valueOf((int)yTile);
  
  return strProjection;
 }
 
 private String genHereMapTileRequest(String pixelCnt){
  
  String BaseURL = "http://1.base.maps.cit.api.here.com";
  String Path = "/maptile/2.1/";
  String Resource = "maptile";
  String Version = "/newest";
  String NormalDay = "/normal.day";
  String ApplicationId = "DemoAppId01082013GAL";  //for demo
  String ApplicationCode = "AJKnXv84fjrb0KIHawS0Tg"; //for demo
  String png8 = "/png8";
  
  int zoom = sbZoom.getProgress();
  //Berlin
  String strZoomColumnRow = getMercatorProjection(52.525439, 13.38727, zoom);

  String rqs = BaseURL + Path + Resource + Version + NormalDay + strZoomColumnRow
    + pixelCnt
    + png8
    + "?app_id=" + ApplicationId
    + "&app_code=" + ApplicationCode;

  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:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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" />

    <SeekBar
        android:id="@+id/sbzoom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="13"
        android:progress="5" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/maprqs512"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/mapimage1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/maprqs256"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/mapimage2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

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

download filesDownload the files.

~ More examples of using Here Map REST API on Android

No comments: