2012-05-06 19 views
2

這是我mapActivity:中的onTap一個谷歌地圖一直被叫做Android

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; 
    } 
} 

一個標記,敲擊時,我想,看到的警告對話框中,如果我按是做一些事情。問題是每當我做Tap時,onTap就會被調用,而不僅僅是標記(如果我按下地圖的無關點,我也會看到提醒對話框)。

任何幫助?

回答

3

您需要覆蓋onTap(int)方法,其中int是覆蓋項目的索引。
onTap(GeoPoint, MapView)文檔:

處理敲打事件。只有在龍頭位於物品上時纔會處理龍頭,並且您已重寫onTap(int)以返回true。

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

0

如果想使用onTap(GeoPoint,MapView)那麼你必須使用region.contains(point.x, point.y)檢查是否給定的抽頭點落入的區域。