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

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

作者: 攻城虎 | 来源:发表于2019-06-05 17:45 被阅读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

    喜欢的话,点个关注

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/rbpsxctx.html