2015-03-17 3 views
5

Ich bin in der Lage, sichtbares Rechteck der Kartenansicht zu erhalten und auch Mittelpunkt der Kartenansicht und Spannweite Deltas werden auch von Mkmaap Ansicht Methoden erhalten: Um sichtbar zu werden sind: mapView.visibleMapRect wird verwendet. Um den Mittelpunkt zu bekommen: map view.centerCoordinate wird benutzt und um den Span zu bekommen: mapView.region.span wird benutzt.Wie bekomme ich den Radius vom sichtbaren Bereich von MKmapview?

Jetzt habe ich alle Informationen, wie kann ich Radius des sichtbaren berechnen berechnen? Kann mir das jemand im Detail erklären?

Ich habe this question gesehen, aber die Antwort gibt mir Spannweite nicht Radius des sichtbaren Bereichs.

Antwort

8

Um den Radius folgen diese:

- (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; 
} 

Es wird der Radius in Meter zurückzukehren.

Zentrum

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

Für immer Radius Um koordinieren, hängt davon ab, wo Sie den zweiten Punkt zu wollen. Lets nehmen Sie die 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]; 
} 
+0

Gut es funktionierte für mich...! –

+0

Für alle Swifties: eine kurze Verlängerung für die MKMapView (hier i für den Radius der oberen linken Ecke bin mit: 'Erweiterung MKMapView { var topLeftCoordinate: CLLocationCoordinate2D { return convert (CGPoint.zero, toCoordinateFrom: Selbst-) } var Radius: CLLocationDistance { lassen centerLocation CLLocation = (Breite: centerCoordinate.latitude, Länge: centerCoordinate.longitude) lassen topLeftLocation CLLocation = (Breite: topLeftCoordinate.latitude, Länge: topLeftCoordinate.longitude) return centerLocation.distance (aus: topLeftLocation) } } ' –

5

Mit Swift 3.0 Sie Erweiterung verwenden können, Ihr Leben zu vereinfachen:

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) 
    } 

} 

Mit 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) 
    } 

} 
+2

** Swift 4 **: Ändern Sie 'CLLocation (Koordinate: *)' in 'CLLocation (latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)' – Michael