不知何故,当我点击添加到地图的图标时,未调用 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);
}
}
}