iOS 高德地图 获取合适的zoomCenter 和 zoomLevel
怪我咯
怪我咯 2017-04-17 15:26:47
[iOS讨论组]

场景:现在有很多骑行记录软件,打开某次记录,会把骑行的路径添加到地图上,路径全部显示在屏幕中并且缩放得刚刚好

问题:在已知多个坐标点的情况下,如何获取这些坐标点全部显示在屏幕上对应地图的中心坐标和合适的缩放比例

想法:是否有一个方法把这些坐标做为参数传进去就可以得到我想要的那两个值呢?

怪我咯
怪我咯

走同样的路,发现不同的人生

全部回复(1)
PHP中文网

可以参考如下代码:

- (void)zoomToMapPoints:(MKMapView*)mapView annotations:(NSArray*)annotations
{
    double minLat = 360.0f, maxLat = -360.0f;
    double minLon = 360.0f, maxLon = -360.0f;
    for (MKPointAnnotation *annotation in annotations) {
        if ( annotation.coordinate.latitude  < minLat ) minLat = annotation.coordinate.latitude;
        if ( annotation.coordinate.latitude  > maxLat ) maxLat = annotation.coordinate.latitude;
        if ( annotation.coordinate.longitude < minLon ) minLon = annotation.coordinate.longitude;
        if ( annotation.coordinate.longitude > maxLon ) maxLon = annotation.coordinate.longitude;
    }
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) / 2.0, (minLon + maxLon) / 2.0);
    MKCoordinateSpan span = MKCoordinateSpanMake(maxLat - minLat, maxLon - minLon);
    MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
    [mapView setRegion:region animated:YES];
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号