Añadir marcador en el lugar afectado en el mapa Google en Android
-
24-09-2019 - |
Pregunta
¿Cómo agrego un marcador en un lugar determinado en el mapa?
vi este código que muestra las coordenadas de la ubicación tocado. Y quiero un marcador para que aparezca o se muestra en ese mismo lugar cada vez que se toca. ¿Cómo hago esto?
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;
}
Solución
Si desea añadir un OverlayItem . El tutorial Google Mapview muestra cómo utilizar a él.
Otros consejos
Si desea agregar un marcador en la ubicación tocado, entonces usted debe hacer lo siguiente:
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;
}
Compruebe que Im llamando MarkerOverlay después aparecerá el mensaje. Con el fin de hacer que esto funciona, usted tiene que crear otra plantilla, 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;
}
}
Espero que encuentre esto! Útil
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow