Add marker on google map on touched location in Android

不问归期 提交于 2019-12-08 05:47:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!