【问题标题】:android google map overlays ontapandroid 谷歌地图覆盖 ontap
【发布时间】:2012-02-12 20:15:36
【问题描述】:
我创建了 3 个类。它们中的每一个都扩展了 Overlay。他们每个人都有“复杂”的覆盖绘制方法。这就是为什么我选择覆盖 Overlay 而不是使用 ItemizedOverlay。现在,为了确定已点击了哪个叠加层,使用 ItemizedOverlay 会更容易,但我仍然使用“普通”叠加层。如何正确确定我的哪些叠加层已被点击。
我已经在每个(三个)扩展 Overlay 的类中重写了 onTap 方法。结果是,无论我在地图上的哪个位置触摸所有三个类的 onTap 都会被调用。
如果我触摸了我的绘图,我是否必须根据 onTap 的方法参数 GeoPoint 和我的当前位置进行计算?如何正确地做到这一点?将所有内容更改为 ItemizedOverlay?如果是这样,一些叠加层的复杂绘图怎么做?
10 倍
问候
【问题讨论】:
标签:
android
google-maps
map
touch
overlay
【解决方案1】:
我已经创建了解决方案:
@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;
}