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.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
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);
  }
 }
 
 private final int optTypeBaseMap = 0;
 private final int optTypeAerialTile = 1;
 private final int optTypeTrafficTile = 2;
 String[] optTypes = {
      "Base Map", 
      "Aerial Tile", 
      "Traffic Tile"};
 
 SeekBar sbZoom;
 Spinner spType;
 TextView textviewMapRqs512, textviewMapRqs256;
 ImageView mapImage1, mapImage2;
 
 ArrayAdapter<String> spTypeAdapter;
 @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();
   }});
  
  spType = (Spinner)findViewById(R.id.sptype);
  spTypeAdapter = new ArrayAdapter<String>(
   this,android.R.layout.simple_spinner_item, optTypes);
  spTypeAdapter.setDropDownViewResource(
    android.R.layout.simple_spinner_dropdown_item);
  spType.setAdapter(spTypeAdapter);
  
  spType.setOnItemSelectedListener(new OnItemSelectedListener(){
   @Override
   public void onItemSelected(AdapterView<?> parent, View view,
     int position, long id) {
    updateHereMap();
   }
   @Override
   public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub
    
   }});
  
  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){
  /*
   * reference:
   * https://developer.here.com/rest-apis/documentation/enterprise-map-tile/topics/resource-map-tile.html
   */
  
  String BaseURL;
  String Path = "/maptile/2.1/";
  String Resource;
  String Version = "/newest";
  String scheme;
  
  int type = spType.getSelectedItemPosition();
  switch(type){
  case optTypeBaseMap:
   BaseURL = "http://1.base.maps.cit.api.here.com";
   Resource = "maptile";
   scheme = "/normal.day";
   break;
  case optTypeAerialTile:
   BaseURL = "http://2.aerial.maps.cit.api.here.com";
   Resource = "maptile";
   scheme = "/satellite.day";
   break;
  case optTypeTrafficTile:
   BaseURL = "http://4.traffic.maps.cit.api.here.com";
   Resource = "traffictile";
   scheme = "/normal.day";
   break;
  default:
   return null;
  }
  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 + scheme + 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" />
    
    <Spinner
        android:id="@+id/sptype"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <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 the files.
Download the files.~ More examples of using Here Map REST API on Android
 
No comments:
Post a Comment