iOS如何让百度地图上所有的点展示在合适的范围之内

2019-06-05  本文已影响0人  攻城虎

其他技术文档提供的也是可以的,不过存在一个边点问题,也就是说一个点刚好在屏幕的边缘,不符合需求 ,话不多说,直接上代码

- (BMKCoordinateRegion)getCoordinateRegionWithPoints:(NSArray <CLLocation *>*)points{
    
    double minLat = 90.0;
    double maxLat = -90.0;
    double minLon = 180.0;
    double maxLon = -180.0;
    for (size_t i = 0; i < points.count; i++) {
        minLat = fmin(minLat, ((CLLocation *)points[i]).coordinate.latitude );
        maxLat = fmax(maxLat,((CLLocation *)points[i]).coordinate.latitude);
        minLon = fmin(minLon, ((CLLocation *)points[i]).coordinate.longitude);
        maxLon = fmax(maxLon, ((CLLocation *)points[i]).coordinate.longitude);
    }
    
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake((minLat + maxLat) * 0.5, (minLon + maxLon) * 0.5);
    CLLocationDegrees latDiff = (center.latitude - minLat) * 2.0;
    CLLocationDegrees longDiff = (center.longitude - minLon) * 2.0;
    
    BMKCoordinateSpan span = {fabs(latDiff)+0.05,fabs(longDiff)+0.05};
    BMKCoordinateRegion region = {center, span};
    
    return region;
}

最主要的设置

    BMKCoordinateSpan span = {fabs(latDiff)+0.05,fabs(longDiff)+0.05};

0.05 是你需要调整的

效果如下


WeChat435ba93f6687b9607303f173d4965aff.png

喜欢的话,点个关注

上一篇下一篇

猜你喜欢

热点阅读