【问题标题】:android detect various circles on a map v2android检测地图v2上的各种圆圈
【发布时间】:2015-02-07 17:06:30
【问题描述】:

在我的小应用程序中,我有一张地图,我正在解析来自网络的 XML,如下所示:

<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>

每次我从 xml 中读取“地点”时,我的 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

【问题讨论】:

  • 你的问题是你没有保留对你生成的圆圈和标记的引用,你最终得到了最后一个引用(因为你只使用了一个引用)。检查此代码 gist.github.com/luksprog/de0fc75ead76f0d8881d 以了解如何操作(尽管使用地理围栏 API 会是更好的选择)。
  • @Luksprog 你的回答对我来说更有用。我会给你150分,但我只有一条评论。请发布答案以给您积分。谢谢!

标签: android google-maps-markers google-maps-android-api-2 android-geofence


【解决方案1】:

最后一个标记(Marker aMarker;)和圆圈(Circle 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; 
} 

【讨论】:

    【解决方案2】:

    【讨论】:

    • 我一直在阅读文档,虽然这是一种正确的方法,但我很想知道是否有任何方法可以利用我编写的代码。使用如何在我的查询中解释。感谢您的关注,谢谢
    【解决方案3】:

    我认为你犯了错误 createNewMarkerAndCircle(),请让 MarkerOptions 和 Circle 本地化,否则更新地图时只显示最后一个实例,这就是只显示最后一个圆圈的原因。

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

    【讨论】:

      猜你喜欢
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      相关资源
      最近更新 更多