不知何故,当我点击我添加到地图上的图标时,没有调用onTap事件,有人可以告诉我原因:S?它覆盖了我添加到地图的所有用户,他们都有相同的图标,但所有不同的标题。但是,当我点击没有任何反应时,我也尝试记录它,如果Log.d发生了什么事,但没有任何反应。这是代码:
private class MyUsersOverlay extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
public MyUsersOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlayItem(int lat, int lon, String title, String... message) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}
public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}
@Override
protected boolean onTap(int index)
{
Log.d("Test Message", "It works");
OverlayItem item = mOverlays.get(index);
//Do stuff here when you tap, i.e. :
//AlertDialog.Builder dialog = new AlertDialog.Builder(cMainActivity);
//dialog.setTitle(item.getTitle());
//dialog.setMessage(item.getSnippet());
//dialog.show();
Toast.makeText(cMainActivity, item.getTitle(),
Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
if (shadow == false)
{
//cycle through all overlays
for (int index = 0; index < mOverlays.size(); index++)
{
OverlayItem item = mOverlays.get(index);
// Converts lat/lng-Point to coordinates on the screen
GeoPoint point = item.getPoint();
Point ptScreenCoord = new Point() ;
mapView.getProjection().toPixels(point, ptScreenCoord);
//Paint
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setTextSize(10);
paint.setARGB(150, 0, 0, 0); // alpha, r, g, b (Black, semi see-through)
//show text to the right of the icon
canvas.drawText(item.getTitle(), ptScreenCoord.x, ptScreenCoord.y+10, paint);
}
}
}
答案 0 :(得分:0)
您是否记得将mapView.setClickable(true);
放入oncreate()
方法?
答案 1 :(得分:0)
如果您想要点击叠加层并且有点击一般地图视图的事件,则可能会出现问题。如果这是您要实现的目标,请查看this tutorial。即使您遇到其他问题,本教程也可以帮助您解决问题。
答案 2 :(得分:0)
问题不在上面的代码中,而是在其他地方。您有另一个视图从您的'ItemizedOverlay`中窃取touch
个事件。
Possibilities
dispatchTouchEvent()
并一直返回true
,告知事件已被使用且不会再发送任何调度。MapView
的布局中,您可能在MapView
之后添加了一个视图覆盖它的视图。从上次添加的视图开始调度运动事件。MapView
。ItemizedOverlay
叠加层