开发者

Add marker on google map on touched location in Android

开发者 https://www.devze.com 2023-04-12 13:21 出处:网络
Can anyone help me in the following task: I want to add a marker in g开发者_JAVA百科oogle map 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号