且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

删除previous标记,并在谷歌地图v2中增加新的标记

更新时间:2023-08-30 07:51:46

刚刚创造一个新的标记对象,并在添加新的标记,删除previous一个

 标记标记;

MAP.setOnMapLongClickListener(新GoogleMap.OnMapLongClickListener(){

                @覆盖
                公共无效onMapLongClick(经纬度为arg0){
                    如果(标记!= NULL){
                        marker.remove();
                    }
                    标记= MAP.addMarker(新MarkerOptions()
                            。位置(
                                    新的经纬度(arg0.latitude,
                                            arg0.longitude))
                            .draggable(真)。可见(真));
                }
            });
 

修改

执行相同的 OnMapClick

  MAP.setOnMapClickListener(新OnMapClickListener(){
        @覆盖
        公共无效onMapClick(经纬度点){
            // TODO自动生成方法存根

                如果(标记!= NULL){
                    marker.remove();
                }
            标记= MAP.addMarker(新MarkerOptions()
                    .position(currentPosition)
                    是.snippet(
                            纬度:+ location.getLatitude()+LNG
                                    + location.getLongitude())
                    .icon(BitmapDesc​​riptorFactory
                            .defaultMarker(BitmapDesc​​riptorFactory.HUE_AZURE))
                    .title伪(ME));
            Log.e(土地增值税,+点);

        }
    });
 

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;

}

Just creat a new marker object and before adding a new marker, remove the previous one

Marker marker;

MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

                @Override
                public void onMapLongClick(LatLng arg0) {
                    if (marker != null) {
                        marker.remove();
                    }
                    marker = MAP.addMarker(new MarkerOptions()
                            .position(
                                    new LatLng(arg0.latitude,
                                            arg0.longitude))
                            .draggable(true).visible(true));
                }
            });

EDIT

Do the same for OnMapClick

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

                if (marker != null) {
                    marker.remove();
                }
            marker = 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);

        }
    });