It's a simple example of using MapView, with everything implement in main activity.
In this article, I'm going to implement a custom MyMapView extends MapView. Most of the routine works will be moved in MyMapView, include MyItemizedOverlay, MyLocationOverlay and some initialization such as setClickable(true), setBuiltInZoomControls(true)...etc. Such that the main activity will become simple and clear.
Modify main.xml to add <com.AndroidCustomMapView.MyMapView> in the layout.
In this article, I'm going to implement a custom MyMapView extends MapView. Most of the routine works will be moved in MyMapView, include MyItemizedOverlay, MyLocationOverlay and some initialization such as setClickable(true), setBuiltInZoomControls(true)...etc. Such that the main activity will become simple and clear.
package com.AndroidCustomMapView;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
public class MyMapView extends MapView {
MyItemizedOverlay myItemizedOverlay = null;
MyLocationOverlay myLocationOverlay = null;
public MyMapView(Context context, String apiKey) {
super(context, apiKey);
init(context);
}
public MyMapView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyMapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context ctx){
setClickable(true);
setBuiltInZoomControls(true);
Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
int markerWidth = marker.getIntrinsicWidth();
int markerHeight = marker.getIntrinsicHeight();
marker.setBounds(0, markerHeight, markerWidth, 0);
myItemizedOverlay = new MyItemizedOverlay(marker);
getOverlays().add(myItemizedOverlay);
myLocationOverlay = new MyLocationOverlay(ctx, this);
getOverlays().add(myLocationOverlay);
}
public void addMarker(GeoPoint p, String title, String snippet){
myItemizedOverlay.addItem(p, title, snippet);
}
public void callOnResume(){
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
public void callOnPause(){
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
public class MyItemizedOverlay extends ItemizedOverlay{
private ArrayListoverlayItemList = new ArrayList ();
public MyItemizedOverlay(Drawable defaultMarker) {
//super(defaultMarker);
super(boundCenterBottom(defaultMarker));
populate();
}
@Override
protected OverlayItem createItem(int i) {
return overlayItemList.get(i);
}
@Override
public int size() {
return overlayItemList.size();
}
public void addItem(GeoPoint p, String title, String snippet){
OverlayItem newItem = new OverlayItem(p, title, snippet);
overlayItemList.add(newItem);
populate();
}
}
}
Modify main.xml to add <com.AndroidCustomMapView.MyMapView> in the layout.
Modify the activity, AndroidCustomMapViewActivity to use our MyMapView. It become much clear now.
Remember to modify AndroidManifest.xml to include uses-library of "com.google.android.maps", and grant permission of "android.permission.INTERNET" and "android.permission.ACCESS_FINE_LOCATION".
package com.AndroidCustomMapView;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
public class AndroidCustomMapViewActivity extends MapActivity {
MyMapView myMapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myMapView = (MyMapView)findViewById(R.id.mymapview);
GeoPoint myPoint1 = new GeoPoint(0*1000000, 0*1000000);
myMapView.addMarker(myPoint1, "myPoint1", "myPoint1");
GeoPoint myPoint2 = new GeoPoint(50*1000000, 50*1000000);
myMapView.addMarker(myPoint2, "myPoint2", "myPoint2");
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
myMapView.callOnPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
myMapView.callOnResume();
}
}