Android programming tutorials, 3rd edition

Page 258

Putting Lunch on the Map

return(true); }

We are passed in the index of the marker the user tapped, which in this case will always be 0 since there is but one marker. Here, we just display a Toast, containing the name of the restaurant, stashed in the OverlayItem's "snippet". You will need to add an import for android.widget.Toast, though. The complete RestaurantMap class should look a wee bit like: package apt.tutorial; import import import import import import import import import

android.graphics.drawable.Drawable; android.os.Bundle; android.widget.Toast; com.google.android.maps.GeoPoint; com.google.android.maps.ItemizedOverlay; com.google.android.maps.MapActivity; com.google.android.maps.MapController; com.google.android.maps.MapView; com.google.android.maps.OverlayItem;

public class RestaurantMap extends MapActivity { public static final String EXTRA_LATITUDE="apt.tutorial.EXTRA_LATITUDE"; public static final String EXTRA_LONGITUDE="apt.tutorial.EXTRA_LONGITUDE"; public static final String EXTRA_NAME="apt.tutorial.EXTRA_NAME"; private MapView map=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); double lat=getIntent().getDoubleExtra(EXTRA_LATITUDE, 0); double lon=getIntent().getDoubleExtra(EXTRA_LONGITUDE, 0); map=(MapView)findViewById(R.id.map); map.getController().setZoom(17); GeoPoint status=new GeoPoint((int)(lat*1000000.0), (int)(lon*1000000.0)); map.getController().setCenter(status); map.setBuiltInZoomControls(true); Drawable marker=getResources().getDrawable(R.drawable.marker); marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());

231


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.