我有一个mapView,其中有几个点用标记绘制,每个标记具有不同的纬度和经度,在附近可能相邻也可能不相近。有没有办法让MapView打开到最深的缩放级别,将屏幕上的所有点集中在一起?
我知道mapController有一个setZoom和setCenter属性,但是如何确定这些方法的值?还是有另一种更简单的方法来实现这个目标吗?
答案 0 :(得分:5)
在MapView中,在ImtemizedOverlay中添加了overlayItem后,如下所示:
GeoPoint aGeoPoint = new GeoPoint((int)(latitude * 1E6), (int)(longitude * 1E6));
OverlayItem aOverlayItem = new OverlayItem(aGeoPoint, "Auto", "Auto");
locationsItemizedOverlay.addOverlay(aOverlayItem);
试试这个:
// Center map in geopoints
GeoPoint point = locationsItemizedOverlay.getCenterPoint();
int latSpan = locationsItemizedOverlay.getLatSpanE6();
int longSpan = locationsItemizedOverlay.getLonSpanE6();
mapView.getController().setCenter(point);
mapView.getController().zoomToSpan((int)(latSpan * Constants.MAP_ZOOM_FACTOR), (int)(longSpan * Constants.MAP_ZOOM_FACTOR));
在重写的ItemizedOverlay中:
public GeoPoint getCenterPoint() {
if(center == null) {
int northEdge = -90000000; // i.e., -90E6 microdegrees
int southEdge = 90000000;
int eastEdge = -180000000;
int westEdge = 180000000;
Iterator<OverlayItem> iterator = overlayItemsArrayList.iterator();
while(iterator.hasNext()) {
GeoPoint geoPoint = iterator.next().getPoint();
if(geoPoint.getLatitudeE6() > northEdge)
northEdge = geoPoint.getLatitudeE6();
if(geoPoint.getLatitudeE6() < southEdge)
southEdge = geoPoint.getLatitudeE6();
if(geoPoint.getLongitudeE6() > eastEdge)
eastEdge = geoPoint.getLongitudeE6();
if(geoPoint.getLongitudeE6() < westEdge)
westEdge = geoPoint.getLongitudeE6();
}
center = new GeoPoint((int)((northEdge + southEdge) / 2), (int)((westEdge + eastEdge) / 2));
}
return center;
}
我希望它有所帮助。