问题
Can anyone help me in the following task:
I want to add a marker in google 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.
回答1:
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.
回答2:
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);
}
});
来源:https://stackoverflow.com/questions/7751986/add-marker-on-google-map-on-touched-location-in-android