当我的位置发生变化时,它会添加另一个标记,但不会删除前一个标记
问题描述:
我正在使用以下代码检查设备的当前位置。并且每当它改变或刷新位置时,它都会添加一个标记而不删除最后一个标记。我如何删除以前的标记。当我的位置发生变化时,它会添加另一个标记,但不会删除前一个标记
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) {
}
});
}
还是有另一种获取当前位置的方法吗?感谢
答
试试这个
申报标记对象为本地
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) {
}
});
}
答
保存它是由addMarker方法返回的制造者对象,那么你可以使用marker.setPosition设置标记的新位置
答
实际发生的是,每当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)); }
这样,地图将被清除,其他标记也将被删除。
通过应用程序回答,抱歉格式化,稍后再编辑。
@FrancisVelasco这种方式是,如果你不想删除其他指标更好。这将只删除位置标记。我正在使用它,它工作得很好。如果使用桌面,我会发布这个。 –
@FrancisVelasco。我正在使用相同的方法。很高兴帮助。 –