2012-02-12 2 views
0

나는 3 개의 클래스를 만들었습니다. 각각은 오버레이를 확장합니다. 그리고 각각은 "복잡한"오버 라이딩 된 draw 메소드를 가지고 있습니다. 그래서 ItemizedOverlay 대신 Overlay를 재정의하기로 결정한 것입니다. 이제 어느 오버레이가 도청 되었는가를 결정하기 위해 ItemizedOverlay를 사용하는 것이 더 쉬울 것이지만 여전히 "보통의"오버레이를 사용하고 있습니다. 내 오버레이 중 어느 것이 두 드렸는지 결정하는 법.android google지도 오버레이 ontap

오버레이를 확장하는 모든 (세 가지) 클래스에서 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; 
}