代码之家  ›  专栏  ›  技术社区  ›  Francis Velasco

当我的位置改变时,它会添加另一个标记,但不会删除前一个标记

  •  0
  • Francis Velasco  · 技术社区  · 7 年前

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

     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) {
                }
            });
        }
    

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

    3 回复  |  直到 7 年前
        1
  •  0
  •   Rissmon Suresh    7 年前

    试试这个

    将标记对象声明为本地

     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) {
            }
            });
            }
    
        2
  •  0
  •   Adolf Dsilva    7 年前

        3
  •  0
  •   Lalit Fauzdar    7 年前

    实际上,无论何时调用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)); }
    这样,地图将被清除,其他标记也将被删除。
    通过应用程序回答,抱歉格式化,稍后将进行编辑。