2015-03-17 9 views
5

Sono in grado di ottenere il rettangolo visibile della vista della mappa e anche il punto centrale della vista della mappa e i delta span sono anche ottenuti dai metodi di visualizzazione mkmaap: Per essere visibili sono: mapView.visibleMapRect viene utilizzato. Per ottenere il punto centrale: map view.centerCoordinate viene utilizzato e per ottenere intervallo: mapView.region.span viene utilizzato.Come ottenere il raggio dall'area visibile di MKmapview?

Ora ho tutte le informazioni, come posso calcolare il raggio di visibile utilizzando il calcolo? Qualcuno può spiegarmi in dettaglio?

Ho visto this question ma la risposta mi sta dando span non il raggio dell'area visibile.

risposta

8

Per ottenere il raggio seguire questo:

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

tornerà il raggio in metri.

Per ottenere coordinare centro

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

Per ottenere raggio, dipende da dove si desidera ottenere il 2 ° punto. Consente di prendere il 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

Buono ha funzionato per me...! –

+0

Per tutti swifties: una breve proroga per il MKMapView (qui sto usando l'angolo topleft per il raggio: 'estensione MKMapView {var topLeftCoordinate: CLLocationCoordinate2D { ritorno convert (CGPoint.zero, toCoordinateFrom: auto) } var raggio: CLLocationDistance { lasciare centerLocation = CLLocation (latitudine: centerCoordinate.latitude, longitudine: centerCoordinate.longitude) lasciare topLeftLocation = CLLocation (latitudine: topLeftCoordinate.latitude, longitudine: topLeftCoordinate.longitude) ritorno centerLocation.distance (da: topLeftLocation) } } –

5

Con Swift 3.0 è possibile utilizzare l'estensione per semplificare la vita:

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

} 

Con 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 **: Modifica 'CLLocation (coordinate: *)' a 'CLLocation (latitude: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)' – Michael

Problemi correlati