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.Spinner;
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);
}
}
String[] optResource = {
"1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16",
};
Spinner spZoom;
ImageView mapImage;
ArrayAdapter<String> spZoomAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapImage = (ImageView)findViewById(R.id.mapimage);
spZoom = (Spinner)findViewById(R.id.spzoom);
spZoomAdapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item, optResource);
spZoomAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spZoom.setAdapter(spZoomAdapter);
spZoom.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
}});
}
private void updateHereMap(){
URL urlTarget;
try {
urlTarget = new URL(genHereMapImageRequest());
new LoadHereMapTask(mapImage).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 width = "1000";
String height = "400";
String strZoom = (String) spZoom.getItemAtPosition(
spZoom.getSelectedItemPosition());
String rqs = BaseURL + Path + Resource
+ "?app_id=" + ApplicationId
+ "&app_code=" + ApplicationCode
+ "&c=" + location
+ "&h=" + height
+ "&w=" + width
+ "&z=" + strZoom;
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" />
<Spinner
android:id="@+id/spzoom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<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 the files.
Related:
- Load Here map image, using REST API
No comments:
Post a Comment