2012-02-12 4 views
0

私は3つのクラスを作成しました。それぞれはオーバーレイを拡張します。そして、それぞれには複雑なオーバーライドされた描画メソッドがあります。そのため、ItemizedOverlayを使用する代わりにオーバーレイをオーバーライドすることを選択します。どのオーバーレイがタップされたかを判断するのに、ItemizedOverlayを使用する方が簡単ですが、それでも "通常の"オーバーレイを使用しています。どのオーバレイがタップされたかを正しく判断する方法。アンドロイドgoogleマップオーバーレイontap

オーバーレイを拡張するすべての(3つの)クラスでonTapメソッドをオーバーライドしました。その結果、マップ上のどこにあっても3つのクラスすべてに触れても、onTapが呼び出されます。

私は図面に触れたかどうかにかかわらず、onTapのメソッド引数GeoPointと私の現在の位置に基づいて計算する必要がありますか?どのようにそれを正しく行うには?すべてをItemizedOverlayに変更しますか?もしそうなら、いくつかのオーバーレイの複雑な描画を行う方法は?

10倍 よろしく

答えて

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; 
} 
関連する問題