2012-02-12 57 views
0

我創建了3個類。它們中的每一個都延伸覆蓋。而且他們每個人都有「複雜」的覆蓋畫法。這就是爲什麼我選擇覆蓋Overlay而不是使用ItemizedOverlay。現在爲了確定哪個覆蓋層已經被挖掘,將會更容易地使用ItemizedOverlay,但仍然使用「正常」覆蓋層。如何正確確定哪些疊加層已被點擊。android谷歌地圖疊加ontap

我已經覆蓋了延伸疊加的每個(三個)類中的onTap方法。結果是,無論在地圖上的哪個位置,我都會觸摸所有三個類的onTap。

我是否必須根據onTap的方法參數GeoPoint和我的當前位置進行計算,如果我觸摸了我的繪圖或不是?如何正確地做到這一點?改變一切到ItemizedOverlay?如果是這樣,如何做一些覆蓋圖的複雜圖紙?

10X 問候

回答

2

我已經創建的解決方案:

@Override 
public boolean onTap(GeoPoint geoPoint, MapView mapView) { 
    // Gets mapView projection to use it with calculations 
    Projection projection = mapView.getProjection(); 
    Point tapPoint = new Point(); 
    // Projects touched point on map to point on screen relative to top left corner 
    projection.toPixels(geoPoint, tapPoint); 

    // Gets my current GeoPoint location 
    GeoPoint myLocationGeoPoint = getLocationGeoPoint(); 

    // Image on the screen width and height 
    int pinWidth = myPin.getWidth(); 
    int pinHeight = myPin.getHeight(); 

    // Variable that handles whether we have touched on image or not 
    boolean touched = false; 

    if (myLocationGeoPoint != null) { 
     Point myPoint = new Point(); 

     // Projects my current point on map to point on screen relative to top left corner 
     projection.toPixels(myLocationGeoPoint , myPoint); 

     // Because image bottom line is align with GeoPoint and this GeoPoint is in the middle of image we have to do some calculations 
     // absDelatX should be smaller that half of image width because as I already said image is horizontally center aligned 
     int absDeltaX = Math.abs(myPoint.x - tapPoint.x); 
     if (absDeltaX < (pinWidth/2)) { 
      // Because image is vertically bottom aligned with GeoPoint we have to be sure that we hace touched above GeoPoint (watch out: points are top-left corner based) 
      int deltaY = myPoint.y - tapPoint.y; 
      if (deltaY < pinHeight) { 
       // And if we have touched above GeoPoint and belov image height we have win! 
       touched = true; 
      } 
     } 
    } 

    if (touched) { 
     Log.d(TAG, "We have been touched! Yeah!"); 
    } 

    return true; 
}