by mehta_bhavesh » Thu Jan 01, 2009 12:56 am
Here's some code that I got working for #2 and some code for adding zoom controls provided by Android.
Don't forget to make your MapView clickable. That was the hardest part to figure out to make the MapView work.
In your MapActivity class
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make myMapView the explicit view of this app
mMapView = new MapView(this, "YourApiKeyHere");
// add zoom controls
int y = 10;
int x = 10;
MapView.LayoutParams lp;
lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT,
MapView.LayoutParams.WRAP_CONTENT,
x, y,
MapView.LayoutParams.TOP_LEFT);
View zoomControls = mMapView.getZoomControls();
mMapView.addView(zoomControls, lp);
mMapView.displayZoomControls(true);
// waypoints overlay
// Enable Sattelite-Mode, so we will see the
// Statue of liberty instantly on the screen
mMapView.setSatellite(true);
setContentView(mMapView);
Drawable marker = getResources().getDrawable(R.drawable.star_big_on);
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight());
mWaypointsOverlay = new WaypointsOverlay(this, marker);
List<Overlay> overlayList = mMapView.getOverlays();
overlayList.add(mWaypointsOverlay);
OverlayItem oi = mWaypointsOverlay.getItem(0);
mWaypointsOverlay.setFocus(oi);
//Get the controller, that is used for translation and zooming
MapController mc = mMapView.getController();
// Translate to the Statue of Liberty
mc.animateTo(oi.getPoint());
//Zoom Very close
mc.setZoom(21);
mMapView.setClickable(true);
mMapView.postInvalidate();
}
The WaypointsOverlay class is:
package com.google.maps.navigator;
import java.util.ArrayList;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class WaypointsOverlay extends ItemizedOverlay<OverlayItem> {
private MapActivity mMapActivity;
private LocationDbAdapter mDbHelper;
private Drawable mMarker;
private ArrayList<OverlayItem> locationList;
public WaypointsOverlay(MapActivity mapActivity, Drawable defaultMarker) {
super(defaultMarker);
mMarker = defaultMarker;
mMapActivity = mapActivity;
mDbHelper = new LocationDbAdapter(mMapActivity);
mDbHelper.open();
locationList = new ArrayList<OverlayItem>();
refreshWaypoints();
// Create each of the overlay items included in this layer.
populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
boundCenterBottom(mMarker);
}
@Override
protected boolean onTap(int i) {
Toast.makeText(mMapActivity, locationList.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
return(true);
}
@Override
protected OverlayItem createItem(int index) {
OverlayItem oi = locationList.get(index);
return oi;
}
@Override
public int size() {
//Return the number of markers in the collection
int size = locationList.size();
return size;
}
private void refreshWaypoints() {
locationList.clear();
Cursor cursor = mDbHelper.fetchAllLocations();
mMapActivity.startManagingCursor(cursor);
if (cursor.moveToFirst()) {
do {
String title = cursor.getString(cursor
.getColumnIndex(LocationDbAdapter.KEY_TITLE));
Double latitude = cursor.getDouble(cursor
.getColumnIndex(LocationDbAdapter.KEY_LAT))* 1E6;
Double longitude = cursor.getDouble(cursor
.getColumnIndex(LocationDbAdapter.KEY_LONG))* 1E6;
GeoPoint geopoint = new GeoPoint(latitude.intValue(), longitude.intValue());
OverlayItem oi = new OverlayItem(
geopoint,
title,
title
);
locationList.add(oi);
} while (cursor.moveToNext());
}
}
}
The mDbHelper instance is used to fetch a cursor that returns all the waypoints stored in SQLite3 database.
Hope this is useful to more people. Still looking for answers to #1, #3, and #4.