【问题标题】:When my location changes it adds another marker but doesn't remove the previous one当我的位置发生变化时,它会添加另一个标记,但不会删除前一个标记
【发布时间】:2017-09-25 03:32:46
【问题描述】:

我正在使用下面的代码来检查设备的当前位置。并且每当它更改或刷新位置时,它都会添加一个标记而不删除最后一个。如何删除以前的标记。

 if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                double latitude = location.getLatitude();
                double longtitude = location.getLongitude();
                LatLng latLng = new LatLng(latitude, longtitude);
                mMap.addMarker(new MarkerOptions().position(latLng).title("You Are Here"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f));
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
            }

            @Override
            public void onProviderEnabled(String s) {
            }

            @Override
            public void onProviderDisabled(String s) {
            }
        });
    }

或者还有其他获取当前位置的方法吗?谢谢

【问题讨论】:

    标签: android google-maps google-maps-api-3 location google-maps-markers


    【解决方案1】:

    试试这个

    将标记对象声明为本地

     private Marker currentMarker;
    

    如下操作:

    if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
            if(currentMarker!=null){
            currentMarker.remove();
            }
            double latitude = location.getLatitude();
            double longtitude = location.getLongitude();
            LatLng latLng = new LatLng(latitude, longtitude);
            currentMarker=mMap.addMarker(new MarkerOptions().position(latLng).title("You Are Here"));
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f));
            }
    
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
            }
    
    @Override
    public void onProviderEnabled(String s) {
            }
    
    @Override
    public void onProviderDisabled(String s) {
            }
            });
            }
    

    【讨论】:

    • @FrancisVelasco 如果您不想删除其他标记,这种方式会更好。这将仅删除位置标记。我正在使用它,效果很好。如果使用桌面,我会发布此内容。
    • @FrancisVelasco。 ..我用的是同样的方法。很高兴它有帮助。
    【解决方案2】:

    保存addMarker方法返回的maker对象,然后可以使用marker.setPosition设置marker的新位置

    【讨论】:

      【解决方案3】:

      实际发生的情况是,每当调用 locationchanged 时,都会放置新标记,但实际上在放置此标记之前,您应该清除地图。
      您应该使用 mMap.clear();,如下面的代码所示。
      public void onLocationChanged(Location location) { mMap.clear(); double latitude = location.getLatitude(); double longtitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longtitude); mMap.addMarker(new MarkerOptions().position(latLng).title("You Are Here")); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f)); }
      这样,地图将被清除,其他标记也将被删除。
      通过应用程序回答,抱歉格式化,稍后会编辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-10
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多