简体   繁体   English

Android Google Maps-删除带有图标的标记

[英]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. 不应使用GoogleMap.clear()方法-地图上还有其他一些我不想刷新的对象。 I have also tried setPosition method and had the very same result. 我也尝试过setPosition方法,并得到了相同的结果。 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. 我使用这种方法来创建标记,并且只是setPosition(new Latlng(...))工作。

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(); 您是否尝试替换了marker.remove(); on setPosition() inside onLongClick() method ? onLongClick()方法内的setPosition()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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