iOS将地图中所有的标注点以合适的视野显示在当前视图中

使用的是百度地图SDK
导入头文件#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>

[_mapView showAnnotations:tempArr animated:YES];
[self mapViewFitAnnotations:tempArr];

  • (void)mapViewFitAnnotations:(NSArray *)annotations
    {
    if (annotations.count < 2) return;

    BMKPointAnnotation *annotation = annotations[0];
    CLLocationCoordinate2D coor = annotation.coordinate;
    BMKMapPoint pt = BMKMapPointForCoordinate(coor);

    CGFloat ltX, ltY, rbX, rbY;

    ltX = pt.x;
    ltY = pt.y;
    rbX = pt.x;
    rbY = pt.y;

    for (int i = 1; i < annotations.count; i++) {
    BMKPointAnnotation *annotation = annotations[i];
    CLLocationCoordinate2D coor = annotation.coordinate;
    BMKMapPoint pt = BMKMapPointForCoordinate(coor);
    if (pt.x < ltX) ltX = pt.x;
    if (pt.x > rbX) rbX = pt.x;
    if (pt.y > ltY) ltY = pt.y;
    if (pt.y < rbY) rbY = pt.y;
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    [_mapView setVisibleMapRect:rect];
    _mapView.zoomLevel = _mapView.zoomLevel - 2;
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容