我有这个方法:
public void crowdArea(){
for (int n=0; n<place.length; n++) {
Location locationFromDB2 = new Location("");
locationFromDB2.setLatitude(latt2[n]);
locationFromDB2.setLongitude(longg2[n]);
total = 0;
double c [][]=new double[100][2];
double p [][]=new double[100][2];
for (int n2 = 0; n2 < latt.length; n2++) {
Location locationFromDB3 = new Location("");
locationFromDB3.setLatitude(latt[n2]);
locationFromDB3.setLongitude(longg[n2]);
float dist = locationFromDB2.distanceTo(locationFromDB3);
if (dist < 500f) {
c [total][0]=latt[n2];
c [total][1]=longg[n2];
p [total][0]=prelatt[n2];
p [total][1]=prelongg[n2];
total++;
}
}
if (total >= 5) {
draw(latt2[n], longg2[n]);
state[n]="yes";
boolean b=bearing2(c,p);
Log.d("diriction is: ", b+"");
}
else{
state[n]="no";
// draw2(latt2[n], longg2[n]);
}
}
}
public void draw(double lat, double lonng){
Circle myCircle;
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
myCircle = map.addCircle(circleOptions);
}
它在我的 map 上画了一个圆圈。 该方法每 15 秒调用一次,但它会在前一个圆上绘制一个圆,依此类推。我想要的是在添加新圆圈之前删除以前的圆圈。
如何做到这一点?
最佳答案
您可以在绘制新圆之前使用map.clear()
清除 map 。
或者,如果您在 map 上绘制了其他对象并且不想删除它们,只想删除之前的圆,您可以这样做:
private Circle myCircle;
// ...
public void draw(double lat, double lonng){
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
if (myCircle != null) {
myCircle.remove();
}
myCircle = map.addCircle(circleOptions);
}
根据您的代码更新进行跟进:
private List<Circle> circles = new ArrayList<>();
public void crowdArea(){
clearCircles();
for (int n=0; n<place.length; n++) {
Location locationFromDB2 = new Location("");
locationFromDB2.setLatitude(latt2[n]);
locationFromDB2.setLongitude(longg2[n]);
total = 0;
double c [][]=new double[100][2];
double p [][]=new double[100][2];
for (int n2 = 0; n2 < latt.length; n2++) {
Location locationFromDB3 = new Location("");
locationFromDB3.setLatitude(latt[n2]);
locationFromDB3.setLongitude(longg[n2]);
float dist = locationFromDB2.distanceTo(locationFromDB3);
if (dist < 500f) {
c [total][0]=latt[n2];
c [total][1]=longg[n2];
p [total][0]=prelatt[n2];
p [total][1]=prelongg[n2];
total++;
}
}
if (total >= 5) {
draw(latt2[n], longg2[n]);
state[n]="yes";
boolean b=bearing2(c,p);
Log.d("diriction is: ", b+"");
}
else{
state[n]="no";
// draw2(latt2[n], longg2[n]);
}
}
}
public void draw(double lat, double lonng){
LatLng lt=new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt) //set center
.radius(500) //set radius in meters
.fillColor(0x40ff0000) //default
.strokeColor(Color.RED)
.strokeWidth(5);
circles.add(map.addCircle(circleOptions));
}
private void clearCircles() {
for (Circle circle : circles) {
circle.remove();
}
circles.clear();
}
关于android - 在 map 上绘制新圆之前先删除先前的圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821142/