Remove previous Marker and add new Marker in Google Map v2

前端 未结 8 744
不思量自难忘°
不思量自难忘° 2021-02-05 07:40

I want to remove a current marker after long click on map anywhere and recreate a new marker at that point. I have cleared google map on long click on map and new marker is crea

相关标签:
8条回答
  • 2021-02-05 08:18

    Please try the blow code :-

    // Global Variable...
    private Marker mPreviousMarker ;
    
         @Override
            public boolean onMarkerClick(Marker marker) {
                if (mPreviousMarker != null) {
                    mPreviousMarker.remove();
    
                }
                mPreviousMarker = googleMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDescriptor));
            }
    

    LatLng :- Your latlong where you want to add and bitmapDescroptor is icon. {Just for understanding}

    0 讨论(0)
  • 2021-02-05 08:21

    Here a simple way You just have to change the position of the marker. Create Global Object as Marker marker;
    After that add marker to map like

    marker = map.addMarker(markerOptions).position(new Latlng(31.647316, 74.763791));  
    

    And after it use marker.setPosition(newlaLng); where you need to add marker.

    0 讨论(0)
提交回复
热议问题