我在项目 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));
}
}
答案 0 :(得分: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()
或animateTo()
函数(例如+0.001或+半径)添加额外的固定值或标记半径,以获得更大的范围以适合您的标记(此将作为填充)。
答案 1 :(得分: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();