ghostrider
ghostrider

Reputation: 5259

onTap of a google Map always gets called android

This is my mapActivity:

public class MapsActivity extends MapActivity 
{    
     MapView mapView; 
     MapController mc;
     GeoPoint p; 
     GeoPoint geopoint;
     GeoPoint geopoint_2;
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_w);


        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        @SuppressWarnings("deprecation")
        View zoomView = mapView.getZoomControls(); 
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);
        mapView.setSatellite(true);
        mc = mapView.getController();
        String coordinates[] = {"38.037132", "24.494019"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(9); 


        geopoint_2 =  new GeoPoint((int) (39.204449 *1E6), (int) (24.307251* 1E6));
        mapView.getOverlays().add( new  DrawableMapOverlay(this,p,R.drawable.pushpin, "test"));
        mapView.getOverlays().add( new  DrawableMapOverlay(this,geopoint_2,R.drawable.pushpin, "test_2"));
        mapView.invalidate();



    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

and this is my DrawableMapOverlay

public class DrawableMapOverlay extends Overlay {

  private static final double MAX_TAP_DISTANCE_KM = 3;
  // Rough approximation - one degree = 50 nautical miles
  private static final double MAX_TAP_DISTANCE_DEGREES = MAX_TAP_DISTANCE_KM * 0.5399568 * 50;
  private final GeoPoint geoPoint;
  private final Context context;
  private final int drawable;
  private final String workerName;
  /**
   * @param context the context in which to display the overlay
   * @param geoPoint the geographical point where the overlay is located
   * @param drawable the ID of the desired drawable
   */
  public DrawableMapOverlay(Context context, GeoPoint geoPoint, int drawable,String workerName) {
    this.context = context;
    this.geoPoint = geoPoint;
    this.drawable = drawable;
    this.workerName = workerName;
  }

  @Override
  public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
    super.draw(canvas, mapView, shadow);

    // Convert geo coordinates to screen pixels
    Point screenPoint = new Point();
    mapView.getProjection().toPixels(geoPoint, screenPoint);
    Paint paint = new Paint();
    // Read the image
    Bitmap markerImage = BitmapFactory.decodeResource(context.getResources(), drawable);
    paint.setStrokeWidth(1);
    paint.setARGB(150, 000, 000, 000);
    paint.setStyle(Paint.Style.STROKE);
    // Draw it, centered around the given coordinates
    canvas.drawBitmap(markerImage,
        screenPoint.x - markerImage.getWidth() / 2,
        screenPoint.y - markerImage.getHeight() / 2, null);
    canvas.drawText(workerName, screenPoint.x- markerImage.getWidth() / 2,  screenPoint.y - markerImage.getHeight() / 2 , paint);
    return true;
  }

  @Override
  public boolean onTap(GeoPoint p, MapView mapView) {
    // Handle tapping on the overlay here
      System.out.println("here is is clicked");
     // final Intent myIntent = new Intent(getApplicationContext(), Places.class);
      new AlertDialog.Builder(context)
              .setTitle("Title")
             .setMessage("Beach")
              .setPositiveButton("Yes",
                      new DialogInterface.OnClickListener() {
                         // @Override
                          public void onClick(DialogInterface dialog, int which) {
                         }
                      })
              .setNegativeButton("No",
                      new DialogInterface.OnClickListener() {
                         // @Override
                          public void onClick(DialogInterface dialog, int which) {
                          }

                      }).show();
    return true;
  }
}

I want when tapping on a marker, see the alert dialog and if i press yes do something. The problem is that the onTap gets called whenever I do Tap, not only on the markers (if I press on an irrelevant point of the map I see the alert dialog, too.)

Any help?

Upvotes: 2

Views: 1347

Answers (2)

ceo
ceo

Reputation: 1

if want use onTap(GeoPoint,MapView) then you have to use region.contains(point.x, point.y) to check whether the given tap point fall into the region.

Upvotes: 0

Urban
Urban

Reputation: 2199

You need to override the onTap(int) method where int is the index of the overlay item.
From the docs of onTap(GeoPoint, MapView):

Handle a tap event. A tap will only be handled if it lands on an item, and you have overridden onTap(int) to return true.

So basically instead of using onTap(GeoPoint,MapView), use onTap(int).

Upvotes: 3

Related Questions