0

在我的小应用程序,我有地图,我从网页解析XML,像这样:每次我读时间Android的检测各界在地图V2

<place> 
<lat>42.827602</lat> 
<lon>-1.663957</lon> 
<place_name>Place one</place_name> 
<snippet>Snippet de place Facebook</snippet> 
<thumb>http://www.animage.com/myicon1.png</thumb> 
</place> 

<place> 
<lat>42.830750</lat> 
<lon>-1.669064</lon> 
<place_name>Place two</place_name> 
<snippet>Snippet de place Twitter</snippet> 
<thumb>http://www.animage.com/myicon2.png</thumb> 
</place> 

<place> 
<lat>42.825333</lat> 
<lon>-1.668232</lon> 
<place_name>Place Three</place_name> 
<snippet>Snippet de place Skype</snippet> 
<thumb>http://www.animage.com/myicon3.png</thumb> 
</place> 

</response> 

“地方“,我的AsyncTask的onPostExecute,调用在我的应用程序的地图上创建一个新的标记和圆的方法

例如,如果xml有七个”位置“,则创建一个新的方法标记和一个圆被称为七次。

Marker aMarker; 
Circle aCircle; 

//... 

public void createNewMarkerAndCircle() { 

    //... 

    aMarker = mMap.addMarker(new MarkerOptions() 
        .position(new LatLng(dLat, dLon)) 
        .title(nombre_punto) 
        .snippet(introduccion_punto) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 


      aCircle = mMap.addCircle(new CircleOptions() 
          .center(new LatLng(dLat, dLon)) 
          .radius(150) 
          .strokeColor(Color.RED) 

} 
//... 

public void onPostExecute(String xml) { 

     xml = stringBuffer.toString(); 
     try { 

      Document doc = parser.getDomElement(xml); 

      NodeList nl0 = doc.getElementsByTagName(KEY_PLACE); 

      Element e = (Element) nl0.item(lap); 

      theLat = parser.getValue(e, KEY_LATITUDE); 
      theLon = parser.getValue(e, KEY_LONGITUDE); 

      //... 

      createNewMarkerAndCircle(); 

     //... 

到目前为止,一切工作正常,在地图上创建标记和圆圈。

我的目的是当地图上的用户进入其中一个圆的半径范围内时,获取里面标记的数据。 我向你展示我在做什么:

我有一个位置的监听者,每次更新时都检查距离当前位置和圆的半径。

GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() { 
      @Override 
      public void onMyLocationChange(Location location) { 
       LatLng loc = new LatLng(location.getLatitude(), location.getLongitude()); 

       float[] distance = new float[2]; 

       try { 

        Location.distanceBetween(
          location.getLatitude(), 
          location.getLongitude(), 
          aCircle.getCenter().latitude, 
          aCircle.getCenter().longitude, distance); 

        if (distance[0] > aCircle.getRadius()) { 

         Log.i("myLogs", "Outside"); 

        } else { 

         String markerTitle; 
         markerTitle = aMarker.getTitle(); 

         Log.i("myLogs", "I´ in the circle" + " " + markerTitle); 

        } 
      //.... 

这工作正常,但有一个大问题。

当我开始走路时,只有在进入从xml解析的最后一个“地点”时才会检测到,因此创建了最后一个标记和圆。

我明白问题是什么,创建的最后一个标记(Marker aMarker;)和圆(Circle aCircle;)是那些赋值的......但我不知道如何解决它。

我希望任何帮助,前几天我正在寻找解决方案,但没有成功。

感谢和问候。

更多信息:

我发现这个办法了,但问题仍然没有完全一样的:

https://gist.github.com/saxman/5347195

+0

您的问题是您不会保留对您生成的圆圈和标记的引用,并且最终引用最后一个引用(因为您只使用引用)。检查这个代码https://gist.github.com/luksprog/de0fc75ead76f0d8881d你可能会这样做(虽然使用geo fencing api会是一个更好的选择)。 – Luksprog 2015-02-09 21:53:38

+0

@Luksprog你的答案是我一直更有用。我会给你150分,但我只有一条评论。请发表一个答案给你的观点。谢谢! – Sergio76 2015-02-13 13:01:46

回答

0

最后一个标记(标记aMarker)和圆(圆aCircle;) 创建的是那些具有分配的值...但我不知道如何 解决它。

为了核对你创建你需要保持他们在某种结构的利益,所有点的距离,最简单的是一个清单,到后来引用它们的听众:

Marker aMarker; 
Circle aCircle; 
// this will hold the circles 
List<Circle> allCircles = new ArrayList<Circle>(); 
// this will hold the markers 
List<Marker> allMarkers = new ArrayList<Marker>(); 

public void createNewMarkerAndCircle() { 
    aMarker = mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(dLat, dLon)) 
      .title(nombre_punto) 
      .snippet(introduccion_punto) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE ))); 
    // store the new marker in the above list 
    allMarkers.add(aMarker); 
    aCircle = mMap.addCircle(new CircleOptions() 
      .center(new LatLng(dLat, dLon)) 
      .radius(150) 
      .strokeColor(Color.RED) 
    // store the created circles 
    allCircles.add(aCircle); 
} 

// in the LocationListener iterate over all stored circles and check the distances against each one of them 
// as you add the circles and markers in the same method you'll have a correspondence between them in the two lists 
// another sensible approach would be to create a custom Area class 
// to hold both the Marker and the circle in one single place 
// so when you find yourself inside a circle the marker will be at the same position in allMarkers 
for (int i = 0; i < allCircles.size(); i++) { 
     Circle c = allCircles.get(i); 
     Location.distanceBetween(location.getLatitude(), location.getLongitude(), c.getCenter().latitude, c.getCenter().longitude, distance); 
    if (distance[0] > c.getRadius()) { 
     Log.i("myLogs", "Outside"); 
    } else { 
     String markerTitle; 
     markerTitle = allMarkers.get(i).getTitle(); 
     Log.i("myLogs", "I´ in the circle" + " " + markerTitle); 
     // you found a circles so you may want to break out of the for loop, break; 
} 
0

我认为你犯了错误createNewMarkerAndCircle(),请使MarkerOptions和Circle本地其他更新地图时仅显示最后一个实例,t帽子是只有最后一个圈子显示的原因。

public void createNewMarkerAndCircle() { 

    //... 

    MarkerOptions aMarker = mMap.addMarker(new MarkerOptions() 
        .position(new LatLng(dLat, dLon)) 
        .title(nombre_punto) 
        .snippet(introduccion_punto) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))); 


    Circle  aCircle = mMap.addCircle(new CircleOptions() 
          .center(new LatLng(dLat, dLon)) 
          .radius(150) 
          .strokeColor(Color.RED) 

}