error on getting intent values in overlay class - android

Working with android project and i got a problem on customized overlay class i cannot get the putextra values from the previous class to this class it shows an error on getIntent()
method can any one help to fix this error here is my code..
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
/**
* Class used to place marker or any overlay items on Map
* */
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
String place_reference;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = getIntent();
i.getStringExtra(place_reference);
Intent intent = new Intent(context, SinglePlaceActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent );
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
// MainActivity.this.finish();
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
and this is my error which i found on my code

getIntent() is a method for Activity.
In your case, I would make the constructor like this:
private Activity activity;
public AddItemizedOverlay(Drawable defaultMarker, Context context, Activity a) {
this(defaultMarker);
this.context = context;
this.activity = a;
}
And then use activity.getIntent();

You need to pass the Activty to your AddItemizedOverlay. Then you can call
activity.getIntent();

I think it would be better to pass your place_reference to the constructor and save it as an instance variable, instead of the whole activity. Obviuosly, this would be better when you only need one extra string, that too when it is used only once in this class. You can try:
String place_reference;
public AddItemizedOverlay(Drawable defaultMarker, String place_reference) {
this(defaultMarker);
this.place_reference = place_reference;
}
// and then use place_reference any where you need

write like
Intent i= context.getIntent();
as your class holding this intent is an inner class.

Related

Multiple pins on Google Maps Android not working

I have been using a previous question and a Google Tutorial to attempt to add multiple pin images on a Google Map view when the current location of the user changes.
Each pin when tapped displays the Longitude and Latitude of the pin.
However currently my code only displays one pin and does not add another image when the location of the user changes. I am relatively new to Android and can't see why this code wouldn't work?
Map.java:
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import android.util.Log;
public class Map extends MapActivity {
private MapView map;
private MapController controller;
private double lon, lat;
private Drawable drawable;
private MapOverlay itemizedoverlay;
private List<Overlay> mapOverlays;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
initMapView();
mapOverlays = map.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.pin);
itemizedoverlay = new MapOverlay(drawable, this);
initMyLocation();
}
private void initMapView() {
map = (MapView) findViewById(R.id.map);
controller = map.getController();
map.setSatellite(true);
map.setBuiltInZoomControls(true);
}
private void initMyLocation() {
final MyLocationOverlay overlay = new MyLocationOverlay(this, map);
overlay.enableMyLocation();
overlay.enableCompass(); // wont work in emulator
overlay.runOnFirstFix(new Runnable() {
public void run() {
// Zoom in to current location - 1 is word view
controller.setZoom(20);
controller.animateTo(overlay.getMyLocation());
lon = overlay.getMyLocation().getLongitudeE6();
lat = overlay.getMyLocation().getLatitudeE6();
setLON(lon);
setLAT(lat);
//-0.959896
//51.742953
Log.w("test-lon", Double.toString(lon/1E6));
Log.w("test-lat", Double.toString(lat/1E6));
GeoPoint point = new GeoPoint((int)(lat),(int)(lon));
mapOverlays.add(itemizedoverlay);
test(point);
}
});
map.getOverlays().add(overlay);
}
public void setLON(double x){ //remove?
lon = x;
}
public void setLAT(double x){ //remove?
lat = x;
}
public void test(GeoPoint px){
OverlayItem overlayitem = new OverlayItem(px,"Test Point" , "Lat: " + Double.toString(lat/1E6) + "\nLon: " + Double.toString(lon/1E6) );
itemizedoverlay.addOverlay(overlayitem);
}
#Override
protected boolean isRouteDisplayed() {
// Required method from MapActivity
return false;
}
}
MapOverlay.java:
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class MapOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mapOverlay = new ArrayList<OverlayItem>();
private Context mContext;
public MapOverlay (Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public MapOverlay(Drawable arg0) {
super(boundCenterBottom(arg0));
}
public void addOverlay(OverlayItem overlay) {
mapOverlay.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlay.get(i);
}
#Override
public int size() {
return mapOverlay.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlay.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
u need implements class LocationListener for listen our location changes and update on the map.
class LocationsListeners implements LocationListener {
#Override
public void onLocationChanged(Location location) {
setLAT(location.getLatitude());
setLON(location.getLongitude());
setGeoPoint(lat, lon);
mapOverlays.add(geoPoint);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
u need method for calc the GeoPoint:
public GeoPoint setGeoPoint(double lat, double lon){
geoPoint = new GeoPoint((int) (lat * 1E6), (int)(lon * 1E6));
return geoPoint;
}
or using Little Fluffy Location - Its library that updates our location and notify u, is broadcast receiver, very simple and fast implementation:
try:
http://code.google.com/p/little-fluffy-location-library/

Error in alert-dialog listener while loading a class

I am working with an android project. Now i am creating a customized overlay class. Inside this class i got a problem on an alert dialog listener. I cannot load any intent inside the listener. What should i do with this ? Here is my code..
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
/**
* Class used to place marker or any overlay items on Map
* */
public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public AddItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public AddItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
if (event.getAction() == 1) {
GeoPoint geopoint = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
// latitude
double lat = geopoint.getLatitudeE6() / 1E6;
// longitude
double lon = geopoint.getLongitudeE6() / 1E6;
//Toast.makeText(context, "Lat: " + lat + ", Lon: "+lon, Toast.LENGTH_SHORT).show();
}
return false;
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(this.context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(AddItemizedOverlay.this,SinglePlaceActivity.class));
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
// MainActivity.this.finish();
}
});
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
}
public void populateNow(){
this.populate();
}
}
and got an error as follows
use Context to start the activity.
Intent intent = new Intent(context, RosettaActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
the first parameter of the new Intent(Context, Class)" should be a context object, is the AddItemizedOverlay derived from Context?

How get direction between two point on google map in android?

I searched for two hours but only found draw Line, i want get direction as this form Click here! I have ten point and when i click the point i want to show direction from my location to the point.
My code look:
package de.vogella.android.locationapi.maps;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class ShowMapActivity extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay,itemizedoverlay_central;
private MyLocationOverlay myLocationOverlay;
private String nameOfOffice_AZ[];
private String addressOfOffice_AZ[];
private String phoneOfOffice_AZ[];
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
Projection projection = mapView.getProjection();
nameOfOffice_AZ= getResources().getStringArray(R.array.NamesOFObject_AZ);
addressOfOffice_AZ = getResources().getStringArray(R.array.AddressOfOffice_AZ);
phoneOfOffice_AZ = getResources().getStringArray(R.array.PhoneOfOffice_AZ);
mapController = mapView.getController();
mapController.setZoom(16); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.office_icon);
Drawable drawable_central = this.getResources().getDrawable(R.drawable.central_office);
itemizedoverlay = new MyOverlays(this, drawable);
itemizedoverlay_central = new MyOverlays(this, drawable_central);
createMarker();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
//CENTRAL OFFICE
GeoPoint p_central = new GeoPoint((int)(40.38292884084829 * 1e6),(int)(49.84223246574 * 1e6));
OverlayItem overlayitem = new OverlayItem(p_central, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay_central.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay_central);
//END CENTRALL OFFICE
GeoPoint p = new GeoPoint((int)(40.376455916943236 * 1e6),(int)(49.84803676605224 * 1e6));
overlayitem = new OverlayItem(p, nameOfOffice_AZ[0] ,addressOfOffice_AZ[0]+"\n"+phoneOfOffice_AZ[0] );
itemizedoverlay.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedoverlay);
}
#Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
#Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
}
and MyOverlays.java
package de.vogella.android.locationapi.maps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
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.MapView;
import com.google.android.maps.OverlayItem;
public class MyOverlays extends ItemizedOverlay<OverlayItem> {
private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
private int index = 0;
private boolean full = false;
private Context mContext;
private OverlayItem previousoverlay;
public MyOverlays(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.mContext = context;
}
private ArrayList< OverlayItem > mOverlays = new ArrayList< OverlayItem >();
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void name() {
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int i) {
//when you tap on the marker this will show the informations provided by you when you create in the
//main class the OverlayItem
OverlayItem item = mOverlays.get(i);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setInverseBackgroundForced(true);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.setCancelable(true);
dialog.setPositiveButton("Go To", new OkOnClickListener());
/// dialog.setNegativeButton("No, no", new CancelOnClickListener());
dialog.show();
// nese bax = new nese();
// bax.oldu(mContext);
return true;
}
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "You clicked yes", Toast.LENGTH_LONG)
.show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(mContext, "You clicked no", Toast.LENGTH_LONG).show();
}
}
public class RoutePath extends MapActivity {
/** Called when the activity is first created. */
MapView mapView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
double src_lat = 25.04202; // the testing source
double src_long = 121.534761;
double dest_lat = 25.05202; // the testing destination
double dest_long = 121.554761;
GeoPoint srcGeoPoint = new GeoPoint((int) (src_lat * 1E6),
(int) (src_long * 1E6));
GeoPoint destGeoPoint = new GeoPoint((int) (dest_lat * 1E6),
(int) (dest_long * 1E6));
DrawPath(srcGeoPoint, destGeoPoint, Color.GREEN, mapView);
mapView.getController().animateTo(srcGeoPoint);
mapView.getController().setZoom(15);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
private void DrawPath(GeoPoint src, GeoPoint dest, int color,
MapView mMapView01) {
// code in section 2.2
}
}
}
Less efforts on search..
Anyway look at Google Driving Directions - MapView overlayed Also The Google Directions API
and using intent
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=xx.xx,xx.xx&daddr=xx.xx,xx.xx"));
startActivity(intent);
This blogpost can be a good startpoint: Android driving direction Route-Path
Worked fine for me.

How to add a marker when I touch the map in android ?

I have a map activity which displays the map, I want to add a marker when I touch on the screen ..
This is my Activity ...
package com.adhamenaya.android;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class MapApp extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private Context context;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context=this;
initLayout();
initMap();
}
private void initLayout(){
mapView = (MapView) findViewById(R.id.mapview);
//blueIcon = (Drawable)this.getResources().getDrawable(R.drawable.blueicon);
}
private void initMap(){
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapController=mapView.getController();
mapController.setZoom(10);// 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0, 0, new GeoUpdateHandler());
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat=(int)(location.getLatitude()*1E6);
int lng=(int)(location.getLongitude()*1E6);
GeoPoint point=new GeoPoint(lat,lng);
//GeoPoint point = new GeoPoint(19240000,-99120000);
mapController.animateTo(point);
mapController.setCenter(point);
Drawable redIcon = context.getResources().getDrawable(R.drawable.redicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "Hello !", "I'm here");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
public boolean onTouchEvent(MotionEvent event){
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(context, x, Toast.LENGTH_LONG).show();
GeoPoint point = mapView.getProjection().fromPixels(x, y);
Drawable redIcon = context.getResources().getDrawable(R.drawable.blueicon);
List<Overlay> mapOverlays = mapView.getOverlays();
MyItemizedOverlay itemizedoverlay = new MyItemizedOverlay(redIcon);
OverlayItem overlayitem = new OverlayItem(point, "New Place", "I clicked here !");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
return false;
}
}
}
Edit : This is the class for ItemizedOverlay, where I have implemented onTap() method , but nothing happens ...
package com.adhamenaya.android;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class MyItemizedOverlay extends ItemizedOverlay{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public MyItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MyItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
#Override
public int size() {
return mOverlays.size();
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
int x=(int)event.getX();
int y=(int)event.getY();
Toast.makeText(mContext, x, Toast.LENGTH_LONG).show();
return super.onTouchEvent(event, mapView);
}
}
Hello, MapView! tutorial covers showing markers on MapView. In the tutorial markers are added as soon as the activity starts, but you can also add markers at later time, for example, when user touches screen. If you try this approach, you'll likely want to override onTap in your subclass of ItemizedOverlay.
Update:
If you follow the tutorial, you'll have your own subclass of ItemizedOverlay. onTap is one of its methods. Now that I look at the documentation, unfortunately onTap(GeoPoint p, MapView mapView) is not public or protected so you cannot override it.
What you can do, is have another overlay, solely for detecting taps. Instead of subclassing ItemizedOverlay, subclass Overlay.
private class TapOverlay extends Overlay {
public boolean onTap(GeoPoint p, MapView mapView) {
// code that adds item in your ItemizedOverlay
// goes here
return true;
}
}

Google Maps Tutorial mContext cannot be resolved

I have successfully completed 5 of the android developers tutorials but I am having a problem with the Google Maps tutorial. I have entered the code letter for letter and ctrl+shift+o to import all the packages and I am still getting an error that mContext cannot be resolved. I'm sure its something simple, I've just been staring at the screen too long to see it.
package com.example.HelloGoogleMaps;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public HelloItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
#Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mOverlays.get(i);
}
#Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(defaultMarker);
mContext = context; //ERROR #1
}
#Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); //ERROR #2
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
}
Assignment somewhere.
Context mContext;

Categories

Resources