简体   繁体   中英

Why is my marker not resetting to null?

I have a map that will only allow 1 marker at a time. But in order to stop that marker being accidentally replaced with another, it needs to be set so that the marker is cleared before another marker can be added.
I am setting the marker via onMapClick and clearing them via onMapLongClick. As it stands at the moment, the clearing and adding of markers works fine, but I cannot seem to set up the map so that you need to clear the map first.
I have tried the solution from check for existing markers but it hasn't worked.
Here is my code for the setup, which currently works by clearing existing marker and adding another without the need to clear the original marker first:

@Override
    public void onMapLongClick(LatLng position) {
        mMap.clear();
        Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
        position = null;
    }

    @Override
    public void onMapClick(LatLng position){
        if (position != null){
            mMap.clear();
            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
        }
        else {

            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));           
        }
    }

But I thought it should be something like:

@Override
    public void onMapLongClick(LatLng position) {
        mMap.clear();
        Toast.makeText(this, "Position Cleared", Toast.LENGTH_SHORT).show();
        position = null;
    }

    @Override
    public void onMapClick(LatLng position){
        if (position == null){
            //mMap.clear();
            mMap.addMarker(new MarkerOptions()
            .position(position)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
        }
        else {
            Toast.makeText(this, "Clear first", Toast.LENGTH_SHORT).show();

        }
    }

But all that does is give me the Toast message and not be able to add a marker at all even on first load of the map.

Any help would be great

Create a reference to the added marker, and remove it before adding it again on a new LatLng position.

private Marker marker;

/**
**
some code here
**
**/

@Override
public void onMapClick(LatLng position) {
    if(marker != null) 
        marker.remove();
    marker = mMap.addMarker(new MarkerOptions().position(position).icon(
            BitmapDescriptorFactory
                    .fromResource(R.drawable.ic_launcher_new)));
}

I realised I was using code incorrectly as such I have fixed the issue as thus:

public void onMapClick(LatLng position){
        if(marker == null) {
               marker = mMap.addMarker(new MarkerOptions()
               .position(position)
               .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher_new)));
            } else {
                Toast.makeText(this, "Please clear previous position before adding another", Toast.LENGTH_SHORT).show();
            }

The app now does as intended. Thank you for your help

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