安卓地图:如何删除以前的圆圈,但保留新的圆圈?



所以我想要在用户当前位置周围有一个圆圈,但是每次我移动时都会创建一个新圆圈,但旧圆圈仍然存在。这导致地图上的大混乱。

LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(2000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult == null) {
return;
}
mLastKnownLocation = locationResult.getLastLocation();
checkLocation = mLastKnownLocation;
LatLng lastKnownLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());
Circle mapCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()))
.radius(20)
.strokeColor(Color.argb(200,0,64,122))
.strokeWidth(5)
.fillColor(Color.argb(100,82,189,236)));
mapCircle.setCenter(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()));
HERE I CALCULATE SOME OTHER STUFF
//                mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
};
mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);

这就是我创建圆圈的方式。现在我尝试添加mapCircle.remove((,但这导致根本没有得到一个cirle。我该怎么做。是的,我已经看过这个问题了: 在不清除地图的情况下从安卓谷歌地图 API v2 中删除一个圆圈,它似乎不能解决我的问题。

您可以将本地mapCircle存储在活动或片段(您现在正在使用哪个(中,然后在添加新之前将其删除

class MyActivity extends Activity {
Circle mapCircle = null;
public someMethod() {
// Inside location callback
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
if (locationResult == null) {
return;
}
mLastKnownLocation = locationResult.getLastLocation();
checkLocation = mLastKnownLocation;
LatLng lastKnownLatLng = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude());
if (mapCircle != null) {
mapCircle.remove();
}
mapCircle = mMap.addCircle(new CircleOptions()
.center(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()))
.radius(20)
.strokeColor(Color.argb(200,0,64,122))
.strokeWidth(5)
.fillColor(Color.argb(100,82,189,236)));
mapCircle.setCenter(new LatLng(mLastKnownLocation.getLatitude(),mLastKnownLocation.getLongitude()));
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新