Add marker on google map on touched location in Android
Can anyone help me in the following task:
I want to add a marker in g开发者_JAVA百科oogle map in android.
The functionality has to be like this that a pop up window have to be shown to add the touched location as a marker.
I was referring the below tutorial in that they add the marker through hard coding.
http://developer.android.com/resources/tutorials/views/hello-mapview.html
I want it that to be done using onclck on the map.
I used Google Maps API v2 and the solution is given below:
googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(point.latitude, point.longitude))
.title("New Marker");
googleMap.addMarker(marker);
System.out.println(point.latitude + "---" + point.longitude);
}
});
In MapView
you must use onTouch
instead of onClick
. The motionEvent
that this event fires, has the touch coordinates so with the getProjection()
method from MapView
you can convert the touch coordinates into lat and long to put the Overlay
(Marker
) on the map.
精彩评论