硬编码setZoom()内的onCreate()感觉非常陈旧,我想由最初具有的MapView设置缩放,直到所有GeoPoints / OverlayItems都显示在地图上,以提升用户体验。
hard-coding the setZoom() within onCreate() feels very antiquated and I'd like to enhance the user experience by initially having the MapView set the zoom until all GeoPoints / OverlayItems are visible on the map.
如何才能做到这一点自动神奇?
How can this be done auto-magically?
有点像这样
int minLat = Integer.MAX_VALUE;
int maxLat = Integer.MIN_VALUE;
int minLon = Integer.MAX_VALUE;
int maxLon = Integer.MIN_VALUE;
for (GeoPoint item : items)
{
int lat = item.getLatitudeE6();
int lon = item.getLongitudeE6();
maxLat = Math.max(lat, maxLat);
minLat = Math.min(lat, minLat);
maxLon = Math.max(lon, maxLon);
minLon = Math.min(lon, minLon);
}
mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
mapController.animateTo(new GeoPoint( (maxLat + minLat)/2,
(maxLon + minLon)/2 ));
编辑:
瑞恩给了一个很好的建议:把一个填充,这样一些点不撒谎的边缘(!感谢瑞安)
edit:
Ryan gave a nice suggestion : to put a padding so that some of the point don't lie on the edges (thanks Ryan!)
double fitFactor = 1.5;
mapController.zoomToSpan((int) (Math.abs(maxLat - minLat) * fitFactor), (int)(Math.abs(maxLon - minLon) * fitFactor));