iOS应用内置地图MKMapView

第一次接触MapKit,功能还是蛮强大的, 没技术亮点, 都是MapKit提供好的接口 。

需求: 根据‘用户当前位置的经纬度’ 和 ‘目的地的经纬度’ 要求在地图上把两点都显示出来  //mapview的region可以控制地图的显示范围。

思路: 根据所有已知地点,摘出最小纬度、最大纬度、最小经度、最大经度然后算出中心点和跨度然后就可设置显示范围。//下方效果图

核心部分 😁客官请笑纳

@property (nonatomic, assign)CLLocationDegrees minLat;

@property (nonatomic, assign)CLLocationDegrees maxLat;

@property (nonatomic, assign)CLLocationDegrees minLon;

@property (nonatomic, assign)CLLocationDegrees maxLon;

- (void)getDisplayRangeWithLocationArray:(NSMutableArray *)locationArray {

    //locationArray 存放经纬度标注数组

    for (NSInteger i = 0; i < locationArray.count; i++) {

        CLLocation *location = locationArray[i];

        if (i == 0) {

            //以第一个坐标点做初始值

            _minLat = location.coordinate.latitude;

            _maxLat = location.coordinate.latitude;

            _minLon = location.coordinate.longitude;

            _maxLon = location.coordinate.longitude;

        } else {

            //对比筛选出最小纬度,最大纬度;最小经度,最大经度

            _minLat = MIN(_minLat, location.coordinate.latitude);

            _maxLat = MAX(_maxLat, location.coordinate.latitude);

            _minLon = MIN(_minLon, location.coordinate.longitude);

            _maxLon = MAX(_maxLon, location.coordinate.longitude);

        }

        //动态的根据坐标数据的区域,来确定地图的显示中心点和缩放级别

        if (locationArray.count > 0) {

            //计算中心点

            CLLocationCoordinate2D centCoor;

            centCoor.latitude = (CLLocationDegrees)((_maxLat + _minLat) * 0.5f);

            centCoor.longitude = (CLLocationDegrees)((_maxLon + _minLon) * 0.5f);

            MKCoordinateSpan span;

            //计算地理位置的跨度

            span.latitudeDelta = _maxLat - _minLat;

            span.longitudeDelta = _maxLon - _minLon;


            // 添加显示范围的边距

            span.latitudeDelta = span.latitudeDelta + span.latitudeDelta * 0.3;

            span.longitudeDelta =  span.longitudeDelta + span.longitudeDelta * 0.3;


            // 坐标区域

            MKCoordinateRegion region = MKCoordinateRegionMake(centCoor, span);

            [self.mapView setRegion:region];

        }

    }

}

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

推荐阅读更多精彩内容