Convert MKCoordinateRegion to MKMapRect

前端 未结 10 672
南笙
南笙 2020-12-02 10:52

I have a square MKMapView in my app, and I wish to set a center point and the exact height/width of the view in meters.

Creating an MKCoordinateRegion and setting th

10条回答
  •  萌比男神i
    2020-12-02 11:19

    To add another implementation to the pile:

    - (MKMapRect)MKMapRectForCoordinateRegion:(MKCoordinateRegion)region
    {
        MKMapPoint a = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
            region.center.latitude + region.span.latitudeDelta / 2, 
            region.center.longitude - region.span.longitudeDelta / 2));
        MKMapPoint b = MKMapPointForCoordinate(CLLocationCoordinate2DMake(
            region.center.latitude - region.span.latitudeDelta / 2, 
            region.center.longitude + region.span.longitudeDelta / 2));
        return MKMapRectMake(MIN(a.x,b.x), MIN(a.y,b.y), ABS(a.x-b.x), ABS(a.y-b.y));
    }
    

    NB: There are many ways to convert between MKMapRect and MKCoordinateRegion. This one certainly is not the exact inverse of MKCoordinateRegionMakeWithDistance(), but approximates it fairly well. So, be careful converting back and forth, because information can be lost.

提交回复
热议问题