1

我在我的项目 mapviewballon 中使用了库名称mapviewballoon

当我在不同位置设置 20,30 个或更多引脚时,它会缩放到中心的最后一个引脚,我想要的是缩放级别使其显示地图视图中的所有引脚。

我该如何解决这个问题,或者计算正确的缩放,目前我给出了 14。

做如下事情:

OverlayItem overlayItem = null;
GeoPoint point = null;
for (int i = 0; i < nearbyObjMapList.size(); i++) {
       if (!nearbyObjMapList.get(i).latitude.equals("0") && !nearbyObjMapList.get(i).longitude.equals("0")) {
           Latitude = Float.parseFloat(nearbyObjMapList.get(i).latitude);
           Longitude = Float.parseFloat(nearbyObjMapList.get(i).longitude);
           point = new GeoPoint((int)(Latitude*1E6),(int)(Longitude*1E6));
           overlayItem = new OverlayItem(point, nearbyObjMapList.get(i).venue_name,                            "");
           itemizedOverlay.addOverlay(overlayItem);
       }

                    }
if (thisState == null) {

      final MapController mc = mapView.getController();
      mc.animateTo(point);
      mc.setZoom(14);

 } else {

      // example restoring focused state of overlays
      int focused;
      focused = thisState.getInt("focused_1", -1);
      if (focused >= 0) {
                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
          }
      focused = thisState.getInt("focused_2", -1);
           if (focused >= 0) {
                 temizedOverlay2.setFocus(itemizedOverlay2.getItem(focused));
            }

  }
4

2 回答 2

1

您需要跨越缩放才能实现这一点,我更新了您的代码以便按如下方式执行(它可能有错误,因为我无法调试它):

int minLat=Integer.MAX_VALUE;
int maxLat=Integer.MIN_VALUE;
int minLng=Integer.MAX_VALUE;
int maxLng=Integer.MIN_VALUE;

OverlayItem overlayItem = null;
GeoPoint point = null;
for (int i = 0; i < nearbyObjMapList.size(); i++) {
       if (!nearbyObjMapList.get(i).latitude.equals("0") && !nearbyObjMapList.get(i).longitude.equals("0")) {
           Latitude = Float.parseFloat(nearbyObjMapList.get(i).latitude);
           Longitude = Float.parseFloat(nearbyObjMapList.get(i).longitude);
           point = new GeoPoint((int)(Latitude*1E6),(int)(Longitude*1E6));
           overlayItem = new OverlayItem(point, nearbyObjMapList.get(i).venue_name,                            "");
           itemizedOverlay.addOverlay(overlayItem);

           maxLat = Math.max((int) (Latitude*1.0E6), maxLat);
           minLat = Math.min((int) (Latitude*1.0E6), minLat);
           maxLng = Math.max((int) (Longitude*1.0E6), maxLng);
           minLng = Math.min((int) (Longitude*1.0E6), minLng);
       }

                    }
if (thisState == null) {

      final MapController mc = mapView.getController();
      mc.zoomToSpan(Math.abs(maxLat-minLat), Math.abs(maxLng-minLng));
      mc.animateTo(new GeoPoint((minLat+maxLat)/2,(minLng+maxLng)/2));

 } else {

      // example restoring focused state of overlays
      int focused;
      focused = thisState.getInt("focused_1", -1);
      if (focused >= 0) {
                itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
          }
      focused = thisState.getInt("focused_2", -1);
           if (focused >= 0) {
                 temizedOverlay2.setFocus(itemizedOverlay2.getItem(focused));
            }

  }

zoomToSpan()注意:您可以在或函数(如 +0.001 或 +radius)处添加额外的固定值或标记animateTo()半径,以获得更大的范围以适合您的标记(这将用作填充)。

于 2012-10-10T07:28:15.413 回答
1

不要使用 setZoom() 并且当您有许多要指向的引脚时使用 zoomToSpan 总是更好。您在 for 循环之后添加以下代码,这对我来说非常适合。

                   if(nearbyObjMapList.length!=0)
                   {
                    mapOverlays.add(itemizedoverlay);
                    // showing all overlay items
                    itemizedoverlay.populateNow();
                   // Adjusting the zoom level so that you can see all the markers on map
                    mv.getController().zoomToSpan(Math.abs( minLat - maxLat ), Math.abs( minLong - maxLong ));
                    // Showing the center of the map
                    mc.animateTo(new GeoPoint((maxLat + minLat)/2, (maxLong + minLong)/2 ));
                   }
                   mv.postInvalidate();
于 2012-10-10T07:34:38.440 回答