Android MapView - 自动设置缩放,直到所有 ItemizedOverlay 都可见 [英] Android MapView -setting zoom automatically until all ItemizedOverlay's are visible

查看:23
本文介绍了Android MapView - 自动设置缩放,直到所有 ItemizedOverlay 都可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 onCreate() 中对 setZoom() 进行硬编码感觉非常过时,我想通过最初让 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 给出了一个很好的建议:放置一个填充,这样一些点就不会位于边缘(感谢 Ryan!)


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

这篇关于Android MapView - 自动设置缩放,直到所有 ItemizedOverlay 都可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆