2010年1月27日 星期三

Google Map View


MapViewGoogleAPI.java:

public class MapViewGoogleAPI extends MapActivity {
List mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
MapController Mcontrol;
LinearLayout linearLayout;
MapView mapView;
ZoomControls mZoom;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mZoom = (ZoomControls) mapView.getZoomControls();
GeoPoint point = new GeoPoint(25043943,121533841);
Mcontrol = mapView.getController();
Mcontrol.animateTo(point);
Mcontrol.setZoom(15);
processThread();
linearLayout.addView(mZoom);
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.androidmarker);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
private void processThread() {
new Thread() {
public void run() {
longTimeMethod();
for(int i = 0 ; i < i =" i+10">
Mcontrol.scrollBy(i, 0);
longTimeMethod();
}
GeoPoint newcenter = mapView.getMapCenter();
Mcontrol.animateTo(newcenter);
for(int k = 0 ; k <> Log.e("forloop", "do i in the forloop?");
Mcontrol.scrollBy(0, k);
}
newcenter = mapView.getMapCenter();
Mcontrol.animateTo(newcenter);
HelloItemizedOverlay newitemizedoverlay = new HelloItemizedOverlay(drawable);
OverlayItem newitem = new OverlayItem(newcenter, "", "");
newitemizedoverlay.addOverlay(newit
em);
mapOverlays.add(newitemizedoverlay);
}
}.start();
}

private void longTimeMethod() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e("mapview", e.getMessage());
}
}
}

HelloItemizedOverlay.java:

public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList mOverlays = new ArrayList();
public HelloItemizedOverlay(Drawable defaultMarker) {
//super(defaultMarker);
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}

@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}

@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}

}

main.xml:


result: