Android MapView -setting zoom automatically until all ItemizedOverlay's are visible

筅森魡賤 提交于 2019-11-26 09:18:45

问题


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?


回答1:


Kinda like this

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



回答2:


There's no magical way to achieve this. I suggest to iterate through all your overlayitems to obtain the center and span of all these items. Then set the center and span accordingly for the map



来源:https://stackoverflow.com/questions/5241487/android-mapview-setting-zoom-automatically-until-all-itemizedoverlays-are-visi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!