0

我正在使用以下代碼檢查設備的當前位置。並且每當它改變或刷新位置時,它都會添加一個標記而不刪除最後一個標記。我如何刪除以前的標記。當我的位置發生變化時,它會添加另一個標記,但不會刪除前一個標記

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

還是有另一種獲取當前位置的方法嗎?感謝

回答

0

試試這個

申報標記對象爲本地

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

@FrancisVelasco這種方式是,如果你不想刪除其他指標更好。這將只刪除位置標記。我正在使用它,它工作得很好。如果使用桌面,我會發布這個。 –

+0

@FrancisVelasco。我正在使用相同的方法。很高興幫助。 –

0

保存它是由addMarker方法返回的製造者對象,那麼你可以使用marker.setPosition設置標記的新位置

0

實際發生的是,每當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)); }
這樣,地圖將被清除,其他標記也將被刪除。
通過應用程序回答,抱歉格式化,稍後再編輯。

相關問題