2010-07-06 14 views
5

का उपयोग करके मीटर में त्रिज्या की गणना करें मुझे केंद्र के मानचित्र से दूरी के (किलोमीटर में) स्क्रीन के दूसरी तरफ जानने की आवश्यकता है, (और यदि ज़ूम बदलता है तो दूरी बदल जाएगी)।मैपकिट

मैं इस समारोह

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

कोई भी विचार मैं यह कैसे कर सकते हैं में इस सुविधा को लागू करने की जरूरत है?

धन्यवाद

उत्तर

17

MKMapView centerCoordinate (CLLocationCoordinate2D) नामक गुण और क्षेत्र (MKCoordinateRegion) है।

typedef struct { 
    CLLocationDegrees latitudeDelta; 
    CLLocationDegrees longitudeDelta; 
}MKCoordinateSpan 

आप एक अन्य बिंदु, centerCoordinate के आधार पर बनाने के लिए सक्षम होना चाहिए, के लिए आप संपत्ति या centerCoordinate अक्षांश मान लीजिए कि, latitudeDelta जोड़कर, और CLLocation की विधि का उपयोग कर दूरी की गणना:

क्षेत्र एक struct कि है
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location 

कुछ इस

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)

+0

यह एक बढ़िया जवाब है। – sudo

+0

महान जवाब! आपका बहुत बहुत धन्यवाद! – Andres

+3

'centerCoordinate.latitude + region.span.latitudeDelta' को सही त्रिज्या प्राप्त करने के लिए' centerCoordinate.latitude + region.span.latitudeDelta/2' होना चाहिए। –

0

क्योंकि आयताकार मंडल नहीं हैं, आपको त्रिज्या नहीं मिल सकता है।

लेकिन आप अभी भी एक क्षेत्र के केंद्र से सबसे दूर की दूरी प्राप्त कर सकते हैं। उपरोक्त के समान विचार का पुन: उपयोग करना लेकिन एक त्वरित विस्तार में एम्बेड किया गया।

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() 
संबंधित मुद्दे