在Android中使用谷歌地图在触摸位置添加标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2171429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add marker on touched location using google map in Android
提问by lulala
How do I add a marker on a particular location in the map?
如何在地图中的特定位置添加标记?
I saw this code that shows the coordinates of the touched location. And I want a marker to pop or be shown in that same location every time it is touched. How do I do this?
我看到这段代码显示了触摸位置的坐标。而且我希望每次触摸标记时都会弹出或显示在同一位置。我该怎么做呢?
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;
}
采纳答案by RickNotFred
You want to add an OverlayItem. The Google Mapview tutorialshows how to use it.
您想添加一个OverlayItem。在谷歌MapView类教程展示了如何使用它。
回答by fergerm
If you want to add a marker into the touched location, then you should do the following:
如果要在触摸位置添加标记,则应执行以下操作:
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;
}
Check that Im calling MarkerOverlay after the message appears. In order to make this works, you have to create another Overlay, MapOverlay:
在消息出现后检查我是否调用了 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;
}
}
I hope you find this useful!
希望这个对你有帮助!