2

可能重复:
Android - 地图覆盖 onTouchEvent / onTap 如何?

我有一个 Android 应用程序,它使用MapView带有Overlay(蓝色图钉)的 a 来显示从建筑物列表中选择的建筑物的位置。当用户点击覆盖时,我想显示一条显示建筑物名称和编号的 toast 消息。我遇到的问题是我onTap()在叠加层中的处理会拾取所有点击事件,无论它们是点击蓝色图钉还是地图上完全不同的地方。

我的代码如下所示:

searchedPlaceOverlay = new SearchedPlaceOverlay(SearchPlaces.chosenPlace.getLocation(), this) {
    public boolean onTap(GeoPoint p, MapView mapView) {
        Toast toast = Toast.makeText(getApplicationContext(), "<building name and #>", Toast.LENGTH_LONG);
        toast.show();
        return true;
    }
};
mv.getOverlays().add(searchedPlaceOverlay);

SearchedPlaceOverlay扩展com.google.android.maps.Overlay并且蓝色大头针在地图上显示良好,这只是被称为的非歧视性质onTap()导致了问题。有没有一种简单的方法来确定是否是专门点击了这个覆盖层?

4

2 回答 2

2

As @tabbykitten said ItemizedOverlay would be simpler, but if you must not use it, you can use the the following approach:

  1. Get coordinates X,Y from your marker
  2. Get bounds from your markwe drawable. Depender on marker type, it may not be centered in geopoint.
  3. Adjust bounds to position on geopoint.
  4. Get x,y coordinates ot point tapped.
  5. Check if it's inside adjusted marker bounds.

example code below:

public boolean onTap(GeoPoint p, MapView mapView) { 
  Projection projection = mapView.getProjection();
  projection.toPixels(yourMarkerGeopoint, pointTap);
  yourMarker.copyBounds(boundsTap);
  boundsTap.offset(pointTap.x, pointTap.y);
  projection.toPixels(geoPoint, pointTap);
  if(boundsTap.contains(pointTap.x, pointTap.y)){
    //you tap on the marker
    return true;
  }
  return false;
}
于 2012-10-07T14:18:11.977 回答
1

有带有 onTap(int index) 的 ItemizedOverlay。把你的覆盖物放在里面。然后你知道被点击的索引。

于 2012-10-07T05:37:11.127 回答