2015-03-17 26 views
5

Jestem w stanie uzyskać widoczny prostokąt widoku mapy, a także punkt środkowy widoku mapy i delty zakresu są również pobierane z metod widoku mkmaap: Aby uzyskać widoczne są: mapView.visibleMapRect jest używany. Aby uzyskać punkt środkowy: map view.centerCoordinate jest używany i aby uzyskać rozpiętość: używany jest mapView.region.span.Jak uzyskać promień z widocznego obszaru MKmapview?

Teraz mam wszystkie informacje, w jaki sposób mogę obliczyć promień widocznego są za pomocą obliczenia? Czy ktoś może mi szczegółowo wyjaśnić?

Widziałem this question, ale odpowiedź daje mi rozpiętość, a nie promień widocznego obszaru.

Odpowiedz

8

Aby uzyskać promień postępuj w ten sposób:

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

on powróci promień w metrów.

Aby dostać centrum koordynowania

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

Uzyskania promień, zależy od tego, gdzie chcesz się dostać 2nd punkt. Weźmy 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

Dobra to działało dla mnie...! –

+0

Dla wszystkich swifties: krótki rozszerzenie dla MKMapView (tutaj używam rzut rożny topLeft o promieniu: 'rozszerzenie MKMapView { var topLeftCoordinate: CLLocationCoordinate2D { powrót nawrócony (CGPoint.zero, toCoordinateFrom: self) } promień var: CLLocationDistance { pozwolić centerLocation = CLLocation (szerokość: centerCoordinate.latitude, długość: centerCoordinate.longitude) pozwolić topLeftLocation = CLLocation (szerokość: topLeftCoordinate.latitude, długość: topLeftCoordinate.longitude) powrotu centerLocation.distance (od: topLeftLocation) } } ' –

5

Z Swift 3.0 można użyć rozszerzenie uprościć swoje życie:

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

} 

Z 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 **: Zmień 'CLLocation (coordinate: *)' na 'CLLocation (szerokość geograficzna: self.centerCoordinate.latitude, longitude: self.centerCoordinate.longitude)' – Michael

Powiązane problemy