Calculate radius in meters using mapkit

I need to know the distance (in kilometers) from the central map to the other side of the screen (and if the zoom changes, the distance will change).

I need to implement this function in this function

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
}

Any ideas how I can do this?

thank

+5
source share
2 answers

MkMapView has properties named centerCoordinate (CLLocationCoordinate2D) and scope (MKCoordinateRegion). A region is a structure that:

typedef struct {
    CLLocationDegrees latitudeDelta;
    CLLocationDegrees longitudeDelta;
}MKCoordinateSpan

You must create another point based on centerCoordinate, say by adding the latitudeDelta property of latitude or centerCoordinate to you and calculate the distance using the CLLocation method:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

Something like that

MkMapView * mapView; // init somewhere
MKCoordinateRegion region = mapView.region;
CLLocationCoordinate2D centerCoordinate = mapView.centerCoordinate;
CLLocation * newLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude+region.span.latitudeDelta longitude:centerCoordinate.longitude] autorelease];
CLLocation * centerLocation = [[[CLLocation alloc] initWithLatitude:centerCoordinate.latitude:longitude:centerCoordinate.longitude] autorelease];
CLLocationDistance distance = [centerLocation distanceFromLocation:newLocation]; // in meters

, (, : MKMapViewDelegate)

+17

, .

. , , .

extension MKCoordinateRegion {
    func distanceMax() -> CLLocationDistance {
        let furthest = CLLocation(latitude: center.latitude + (span.latitudeDelta/2),
                             longitude: center.longitude + (span.longitudeDelta/2))
        let centerLoc = CLLocation(latitude: center.latitude, longitude: center.longitude)
        return centerLoc.distanceFromLocation(furthest)
    }
}

let radius = mapView.region.distanceMax()
0

All Articles