6

I am able to get visible rectangle of map view and also centre point of map view and span deltas are also get from mkmaap view methods: To get visible are :mapView.visibleMapRect is used . To get centre point: map view.centerCoordinate is used and to get span: mapView.region.span is used.

Now I have all the information, how can i calculate radius of visible are using calculation? Can anyone explain me in detail?

I have seen this question but the answer is giving me span not radius of visible area.

Community
  • 1
  • 1
Pooja Shah
  • 947
  • 16
  • 45

2 Answers2

10

To get the radius follow this:

- (CLLocationDistance)getRadius
{
    CLLocationCoordinate2D centerCoor = [self getCenterCoordinate];
    // init center location from center coordinate
    CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:centerCoor.latitude longitude:centerCoor.longitude];

    CLLocationCoordinate2D topCenterCoor = [self getTopCenterCoordinate];
    CLLocation *topCenterLocation = [[CLLocation alloc] initWithLatitude:topCenterCoor.latitude longitude:topCenterCoor.longitude];

    CLLocationDistance radius = [centerLocation distanceFromLocation:topCenterLocation];

    return radius;
}

It will return the radius in metres.

To get center coordinate

- (CLLocationCoordinate2D)getCenterCoordinate
{
    return [self.mapView centerCoordinate];
}

For getting radius, depends on where you want to get the 2nd point. Lets take the Top Center

- (CLLocationCoordinate2D)getTopCenterCoordinate
{
    // to get coordinate from CGPoint of your map
    return [self.mapView convertPoint:CGPointMake(self.mapView.frame.size.width / 2.0f, 0) toCoordinateFromView:self.mapView];
}
vivek bhoraniya
  • 1,516
  • 2
  • 17
  • 35
Mohd Prophet
  • 1,503
  • 10
  • 17
  • For all swifties: a short extension for the MKMapView (here i'm using the topleft corner for radius: `extension MKMapView{ var topLeftCoordinate: CLLocationCoordinate2D{ return convert(CGPoint.zero, toCoordinateFrom: self) } var radius: CLLocationDistance{ let centerLocation = CLLocation(latitude: centerCoordinate.latitude, longitude: centerCoordinate.longitude) let topLeftLocation = CLLocation(latitude: topLeftCoordinate.latitude, longitude: topLeftCoordinate.longitude) return centerLocation.distance(from: topLeftLocation) } }` – Finn Fahrenkrug Feb 21 '17 at 14:46
10

With Swift 3.0 you can use extension to simplify your life:

extension MKMapView {

    func topCenterCoordinate() -> CLLocationCoordinate2D {
        return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
    }

    func currentRadius() -> Double {
        let centerLocation = CLLocation(coordinate: self.centerCoordinate)
        let topCenterCoordinate = self.topCenterCoordinate()
        let topCenterLocation = CLLocation(coordinate: topCenterCoordinate)
        return centerLocation.distance(from: topCenterLocation)
    }

}

With Swift 4.0 :

extension MKMapView {

    func topCenterCoordinate() -> CLLocationCoordinate2D {
        return self.convert(CGPoint(x: self.frame.size.width / 2.0, y: 0), toCoordinateFrom: self)
    }

    func currentRadius() -> Double {
        let centerLocation = CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)
        let topCenterCoordinate = self.topCenterCoordinate()
        let topCenterLocation = CLLocation(latitude: topCenterCoordinate.latitude, longitude: topCenterCoordinate.longitude)
        return centerLocation.distance(from: topCenterLocation)
    }

}
Climbatize
  • 1,073
  • 19
  • 31
Luca Davanzo
  • 19,947
  • 14
  • 113
  • 144
  • 2
    **Swift 4**: Change `CLLocation(coordinate: *)` to `CLLocation(latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)` – Michael Sep 15 '17 at 12:32