在地图上绘制新圆之前删除上一个圆

时间:2016-04-24 09:02:30

标签: android google-maps android-studio geometry

我有这个方法:

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

在我的地图上绘制一个圆圈。 此方法每15秒调用一次,但在前一个圆圈上绘制一个圆圈,依此类推。我想要的是在添加新的圆圈之前删除之前的圆圈。

怎么做?

2 个答案:

答案 0 :(得分:2)

我建议您使用

if(mapCircle!=null){
      mapCircle.remove();
    }

而不是map.clear(),因为这会删除地图上的所有对象

答案 1 :(得分:1)

在绘制新圆圈之前,您可以使用map.clear()清除地图。

或者,如果您在地图上绘制了其他对象而又不想删除它们,只需删除之前的圆圈,即可:

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:

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