简体   繁体   中英

Android Google Maps - remove marker with icon

I want to remove a Marker on long click, and place it in another place without clearing the whole map.

Right now the circle radius works fine, but the custom pin icon is not removed. using the GoogleMap.clear() method should not be used - there are some other objects on the map I don't want refreshed. I have also tried setPosition method and had the very same result. How to erase this icon?

Marker declaration:

marker = googleMap.addMarker(new MarkerOptions().position(marker.getPosition()).draggable(true).title("INFO")
                    .infoWindowAnchor(0.5f,0.5f).visible(false)
                    .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.pin_icon))));


this.googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng latLng) {
                if (marker != null) {
                    marker.remove();
                }

                createMarker(latLng);

            }
        });


private void createMarker(LatLng latLng) {
        marker = googleMap.addMarker(new MarkerOptions().position(latLng).draggable(true).title("INFO")
                .infoWindowAnchor(0.5f,0.5f)
                .icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_pin_gr))));

    }

I used such approach for creating markers and simply setPosition(new Latlng(...)) working.

marker = mGoogleMap.addMarker(new MarkerOptions()
                    .anchor(0.5f, 0.5f)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin))
                    .position(new LatLng(cab.getLatitude(), cab.getLongitude())));

Did you try to replace marker.remove(); on setPosition() inside onLongClick() method ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM