gpt4 book ai didi

java - Android map : how to remove previous circle, 但保留新 map 吗?

转载 作者:行者123 更新时间:2023-12-01 19:26:38 25 4
gpt4 key购买 nike

所以我想要围绕用户当前位置形成一个圆圈,但目前每次我移动时都会创建一个新圆圈,但旧圆圈仍然存在。这会导致 map 上一片困惑。

        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() 但这导致根本没有得到圆圈。我怎样才能做到这一点。是的,我已经看过这个问题: Remove a circle from Android Google Maps API v2 without clearing map而且它似乎并没有解决我的问题。

最佳答案

您可以将本地 mapCircle 存储在 Activity 或 Fragment(您现在使用的是哪一个)中,然后在添加新的之前将其删除

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

关于java - Android map : how to remove previous circle, 但保留新 map 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59308397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com