Remove previous Marker and add new Marker in Googl

2020-05-25 23:08发布

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 created but the previous marker also displayed.

My Code is:

public class EditLocation extends Fragment {

View v;
Context c;
GoogleMap MAP;
Button back;
int loc;
String lat;
boolean isTapped = true;

public EditLocation(Context c, int location, String latitude) {
    // TODO Auto-generated constructor stub
    this.c = c;
    this.loc = location;
    this.lat = latitude;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.map, container, false);

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(c);
    if (status != ConnectionResult.SUCCESS) {
        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,
                (Activity) c, requestCode);
        dialog.show();
    } else {
        FragmentManager myFM = ((FragmentActivity) c)
                .getSupportFragmentManager();
        final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                .findFragmentById(R.id.fragmentmap);

        MAP = myMAPF.getMap();

        MAP.setMyLocationEnabled(true);

        LocationManager locationManager = (LocationManager) c
                .getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();

        String provider = locationManager.getBestProvider(criteria, true);

        final Location location = locationManager
                .getLastKnownLocation(provider);
        final LatLng currentPosition = new LatLng(location.getLatitude(),
                location.getLongitude());

        MAP.setOnMapClickListener(new OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                // TODO Auto-generated method stub

                MAP.addMarker(new MarkerOptions()
                        .position(currentPosition)
                        .snippet(
                                "Lat:" + location.getLatitude() + "Lng:"
                                        + location.getLongitude())
                        .icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                        .title("ME"));
                Log.e("lat", "" + point);

            }
        });

        MAP.setOnMapLongClickListener(new OnMapLongClickListener() {

            @Override
            public void onMapLongClick(LatLng point) {
                // TODO Auto-generated method stub

                // isTapped = false;
                MAP.clear();

                MAP.addMarker(new MarkerOptions().position(point)

                .title(point.toString()));

            }

        });

    }

    return v;

}

7条回答
我欲成王,谁敢阻挡
2楼-- · 2020-05-25 23:49

Just clear the google map before adding marker. Like this:

@Override
public void onMapLongClick(LatLng latLng) {
    googleMap.clear();

    googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(latLng.toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
查看更多
登录 后发表回答