谷歌地图的onTap总是被称为android

时间:2012-05-06 21:01:46

标签: android google-maps google-maps-markers

这是我的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;
    }

这是我的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;
  }
}

我想在点击标记时,看到警告对话框,如果我按是做某事。问题在于每当我点击时都会调用onTap,不仅仅是在标记上(如果我按下地图的不相关点,我也会看到警告对话框。)

任何帮助?

2 个答案:

答案 0 :(得分:3)

您需要覆盖onTap(int)方法,其中int是叠加项的索引 来自onTap(GeoPoint, MapView)的文档:

  

处理点击事件。只有当它落在一个项目上时才会处理一个tap,并且你已经覆盖了onTap(int)以返回true。

所以基本上不使用onTap(GeoPoint,MapView),而是使用onTap(int)

答案 1 :(得分:0)

如果要使用onTap(GeoPoint,MapView),则必须使用region.contains(point.x, point.y)检查给定的分享点是否属于该区域。