2016-04-24 65 views
1

上绘制新的之前我有这样的方法:删除以前的圆圈标示

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秒调用一次,但它在前一个圆上绘制一个圆,等等。我想要的是删除之前的圈子,然后再添加新的圈子。

该怎么办?

回答

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

后续根据您的代码更新:

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

但这也会清除地图上的所有标记 – LamaTat

+0

是否有一种方法可以保留标记并仅删除圆圈? – LamaTat

+0

好吧,它工作正常。但是每次调用方法时我都绘制了多个圆圈,使用这将只保留一个圆圈。你可以请检查我的代码我更新它。 – LamaTat

2

我建议你使用

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

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

+0

哪里应该放置此代码?在方法里面? – LamaTat

+0

把你的本地变量myCircle转换成一个字段,然后把它放在你的方法 –

+0

开始工作正常,但是这只保留了一个圆圈。而该方法每隔15秒被调用一次以上并绘制多个圆圈。你可以请检查我的代码我更新它。我想保留最后多个绘制的圆圈。 – LamaTat