android标记remove()方法不起作用

kem*_*mdo 3 android google-maps-markers

我使用 List 来存储标记以添加到 gmap 并自行删除组件。

List<Location> listLocation = new ArrayList<Location>();
Run Code Online (Sandbox Code Playgroud)

位置更新时。我将位置存储到列表位置,删除旧标记。然后将最新位置添加到 Gmap。

     @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            countUpdatePos++;

        listLocation.add(location);
        LatLng lastLocation = new LatLng(location.getLatitude(),
                location.getLongitude());
        gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastLocation, 16));
        String cityName = null;
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        List<Address> addresses = null;
        try {
            addresses = gcd.getFromLocation(location.getLatitude(),
                    location.getLongitude(), 1);
            if (addresses.size() > 0) {
                int a = addresses.get(0).getMaxAddressLineIndex();
                for (int i = 0; i < a; i++) {
                    if (i == 0) {
                        cityName = addresses.get(0).getAddressLine(i);
                    } else {
                        cityName = cityName + ", "
                                + addresses.get(0).getAddressLine(i);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (countUpdatePos == 1) {
            Toast.makeText(getApplicationContext(),
                    getResources().getString(R.string.updated_position), 2000)
                    .show();
            progressToLoadMap.setVisibility(View.GONE);
            checkStartActivity = true;
            timer = new MyCount((int) totalTime * 60 * 1000, 1000);
            timer.start();
        }

        if (listLocation.size() > 1) {
            listLocation.add(location);
//          gmap.clear();
            LatLng newLocation = new LatLng(location.getLatitude(),
                    location.getLongitude());
            gmap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLocation, 16));

            myMarker = gmap.addMarker(new MarkerOptions().position(new LatLng(
                    listLocation.get(listLocation.size() - 1).getLatitude(),
                    listLocation.get(listLocation.size() - 1).getLongitude())));
            if (myMarker != null) {
                myMarker.remove();
                myMarker = null;
            }
            gmap.addMarker(new MarkerOptions()
                    .title(getResources().getString(
                            R.string.current_location_found)).snippet(cityName)
                    .position(newLocation));
            if (listLocation.get(listLocation.size() - 1) != null
                    && listLocation.get(listLocation.size() - 1) != null) {
                gmap.addPolyline(new PolylineOptions()
                        .add(new LatLng(listLocation.get(
                                listLocation.size() - 1).getLatitude(),
                                listLocation.get(listLocation.size() - 1)
                                        .getLongitude()),
                                new LatLng(location.getLatitude(), location
                                        .getLongitude())).width(3)
                        .color(Color.BLUE));
            }
Run Code Online (Sandbox Code Playgroud)

但是当运行活动时,所有标记仍然显示,并且没有折线添加到 gmap :(。帮助我这里是图片 在此处输入图片说明

A.A*_*ada 5

我遇到了同样的问题,在检查我的代码后,我发现我正在调用在 oncreate 和 onresume 事件中两次添加标记的方法,因此在地图上的相同位置添加了 2 个标记,因此当我使用标记删除标记时。 remove() 方法一个被删除,但另一个保留,所以我认为它没有被删除。我从 oncreate 事件中删除了方法调用,现在它工作正常。