在Android中使用谷歌地图在触摸位置添加标记
如何在地图中的特定位置添加标记 我看到这个代码显示了被触摸位置的坐标。我希望每次触摸时都能在同一位置弹出或显示一个标记。我该怎么做在Android中使用谷歌地图在触摸位置添加标记,android,google-maps,google-maps-markers,Android,Google Maps,Google Maps Markers,如何在地图中的特定位置添加标记 我看到这个代码显示了被触摸位置的坐标。我希望每次触摸时都能在同一位置弹出或显示一个标记。我该怎么做 public boolean onTouchEvent(MotionEvent event, MapView mapView) { if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels(
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
mapView.invalidate();
}
return false;
}
您想添加一个。显示了如何使用它。如果要将标记添加到触摸位置,则应执行以下操作:
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
mapView.getOverlays().add(new MarkerOverlay(p));
mapView.invalidate();
}
return false;
}
检查消息出现后我是否正在调用MarkerOverlay。
为了使其正常工作,您必须创建另一个覆盖,MapOverlay:
class MarkerOverlay extends Overlay{
private GeoPoint p;
public MarkerOverlay(GeoPoint p){
this.p = p;
}
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when){
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
mapView.getProjection().toPixels(p, screenPts);
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), /*marker image*/);
canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
return true;
}
}
我希望你觉得这个有用 这回答了你的问题吗?