在地图上绘制新的圆之前删除前一个圆 [英] Delete previous circle before drawing new ones on the map

查看:139
本文介绍了在地图上绘制新的圆之前删除前一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个方法:

pre $ public void crowdArea(){

for(int n = 0; n 位置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 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];

总++;
}
}
if(total> 5){
draw(latt2 [n],longg2 [n]);
state [n] =是;
布尔型b =轴承2(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)//设置中心
.radius(500)//以米为单位设置半径
.fillColor(0x40ff0000)//默认
.strokeColor(Color.RED)
.strokeWidth(5);
myCircle = map.addCircle(circleOptions);
}

在我的地图上绘制一个圆圈。
此方法每15秒调用一次,但它在前一个圆上绘制一个圆,等等。我想要的是删除之前的圈子,然后再添加新的圈子。



如何做到这一点? 您可以使用 map.clear()在绘制新圆之前清除地图。



或者,如果您绘制了其他对象你可以这样做:

  private Circle myCircle; 

// ...

public void draw(double lat,double lonng){
LatLng lt = new LatLng(lat,lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt)//设置中心
.radius(500)//以米为单位设置半径
.fillColor(0x40ff0000)//默认
.strokeColor(Color.RED)
.strokeWidth(5);
if(myCircle!= null){
myCircle.remove();
}
myCircle = map.addCircle(circleOptions);
}

基于您的代码更新的FOLLOWUP:

 私人列表< Circle> circles = new ArrayList<>(); 

public void crowdArea(){
clearCircles();

for(int n = 0; 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 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];

总++;
}
}
if(total> 5){
draw(latt2 [n],longg2 [n]);
state [n] =是;
布尔型b =轴承2(c,p);
Log.d(diriction is:,b +);
}
else {
state [n] =no;

// draw2(latt2 [n],longg2 [n]);




$ b public void draw(double lat,double lonng){
LatLng lt = new LatLng(lat, lonng);
CircleOptions circleOptions = new CircleOptions()
.center(lt)//设置中心
.radius(500)//以米为单位设置半径
.fillColor(0x40ff0000)//默认
.strokeColor(Color.RED)
.strokeWidth(5);
circles.add(map.addCircle(circleOptions));


private void clearCircles(){
for(Circle circle:circles){
circle.remove();
}
circles.clear();
}


I have this method:

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

which draws a circle on my map. this method is called every 15 secs, but it draws a circle on the previous circle and so on. what I want is to remove previous circles before adding the new one.

how to do this?

解决方案

You can use map.clear() to clear the map before drawing the new circle.

Alternatively, if you have drawn other objects on the map and you don't want to delete them, just your previous Circle, you can do:

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

FOLLOWUP based on your code update:

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

这篇关于在地图上绘制新的圆之前删除前一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆