2012-03-07 17 views
35

के बजाय पते का उपयोग करके MKMapView जगह एनोटेशन मैं अक्षांश और देशांतर का उपयोग कर अपने MKMapView पर एनोटेशन लगाने में सक्षम हूं, हालांकि, मेरी फ़ीड में आने के लिए मुझे स्थान का उपयोग करने की आवश्यकता है, इसके बजाय सड़क पते का उपयोग करना है लेट और लांग उदाहरण के लिए 1234 पश्चिम 1234 पूर्व, सैन फ्रांसिस्को, सीए ...आईओएस - अक्षांश और देशांतर

क्या CLLocationManager के साथ इसका कुछ संबंध होगा?

क्या किसी ने इससे पहले प्रयास किया है?

+0

क्या आपका मतलब सड़क का पता है? मुझे यकीन नहीं है कि आपका मतलब क्या है "1234 पश्चिम 1234 पूर्व"। – QED

+0

हां, सड़क के पते से। मैं राज्य, शहर और पता प्राप्त कर सकता हूं। यदि संभव हो, तो मैं उस सड़क पते को लेट और लांग में बदलना चाहता हूं। – Romes

उत्तर

73

psoft की उत्कृष्ट जानकारी के आधार पर, मैं इस कोड के साथ जो खोज रहा था उसे प्राप्त करने में सक्षम था।

NSString *location = @"some address, state, and zip"; 
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
      [geocoder geocodeAddressString:location 
       completionHandler:^(NSArray* placemarks, NSError* error){ 
        if (placemarks && placemarks.count > 0) { 
         CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult]; 

         MKCoordinateRegion region = self.mapView.region; 
         region.center = placemark.region.center; 
         region.span.longitudeDelta /= 8.0; 
         region.span.latitudeDelta /= 8.0; 

         [self.mapView setRegion:region animated:YES]; 
         [self.mapView addAnnotation:placemark]; 
        } 
       } 
      ]; 
+7

दिखाए गए क्षेत्र.सेंटर को सेट करना बहिष्कृत है और वर्तमान अवधि के 1/8 तक अवधि को सेट करना हमेशा समझ में नहीं आता है। यह आसान होगा: 'एमके कॉर्डिनेट क्षेत्र क्षेत्र = एमके कॉर्डिनेट रीजन मेकविथडिस्टेंस (प्लेमार्कमार्ककोर्डिनेट, 5000, 5000); '(5000 मीटर में है)। – Anna

+1

हमेशा ऑब्जेक्ट के बजाय 'firstObject' का उपयोग करें' ऑब्जेक्टएटएक्स: 0' – Laszlo

+1

बस एक साइड नोट के रूप में, 'if (placemarks && placemarks.count> 0) 'अनावश्यक है क्योंकि' placemarks.count' '0' वापस लौटाएगा यदि प्लेसमार्क' शून्य है '। इसे 'if (placemarks.count> 0)' से बदला जा सकता है। – hhanesand

1

यह संभव नहीं है। लेकिन आप CLGeocoder कक्षा का उपयोग करके पते से लंबे/लेट प्राप्त कर सकते हैं।

10

आपको बाद में क्या है geocoding या आगे-geocoding कहा जाता है। रिवर्स-जियोकोडिंग एक लेट/लम्बी जोड़ी को सड़क के पते में बदलने की प्रक्रिया है।

आईओएस 5 geocoding के लिए CLGeocoder कक्षा प्रदान करता है। एमकेप्लेसमार्क आईओएस> = 3.0 में रिवर्स-गोकोडिंग का समर्थन करता है। पाठ्यक्रम में शामिल डेटा बहुत बड़ा है, इसलिए आपके ऐप को आम तौर पर कार्यक्षमता तक नेटवर्क पहुंच की आवश्यकता होगी।

ऐप्पल के Location Awareness Programming Guide शुरू करने के लिए एक अच्छी जगह है। इसके अलावा, यहां SO पर इस बारे में बहुत सारे प्रश्न हैं। https://stackoverflow.com/search?q=geocoding

शुभकामनाएं!

7

iOS 7 के बाद से, placemark.region.center मान्य नहीं है। अभी सेवा का उपयोग करने की जरूरत है:

region.center = [(CLCircularRegion *)placemark.region center]; 

तुम भी पढ़ सकते हैं एप्पल प्रलेखन about this और here भी।

7

स्विफ्ट संस्करण

 let location = self.txtLocation.text; 
     let geocoder:CLGeocoder = CLGeocoder(); 
     geocoder.geocodeAddressString(location!) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in 
      if placemarks?.count > 0 { 
       let topResult:CLPlacemark = placemarks![0]; 
       let placemark: MKPlacemark = MKPlacemark(placemark: topResult); 

       var region: MKCoordinateRegion = self.mkMapView.region; 
       region.center = (placemark.location?.coordinate)!; 
       region.span.longitudeDelta /= 8.0; 
       region.span.latitudeDelta /= 8.0; 
       self.mkMapView.setRegion(region, animated: true); 
       self.mkMapView.addAnnotation(placemark); 

      } 
     } 
9

रिफैक्टर्ड स्विफ्ट संस्करण:

let location = "some address, state, and zip" 
let geocoder = CLGeocoder() 
geocoder.geocodeAddressString(location) { [weak self] placemarks, error in 
    if let placemark = placemarks?.first, let location = placemark.location { 
     let mark = MKPlacemark(placemark: placemark) 

     if var region = self?.mapView.region { 
      region.center = location.coordinate 
      region.span.longitudeDelta /= 8.0 
      region.span.latitudeDelta /= 8.0 
      self?.mapView.setRegion(region, animated: true) 
      self?.mapView.addAnnotation(mark) 
     } 
    } 
} 
2

यहाँ एक और संस्करण है ...

- स्विफ्ट 2017 वाक्य रचना

- एक जगह का नाम बिंदु के लिए दिखाता है, नीचे

नीचे

- 5 किमी कहें, किसी भी आकार का चयन करें,

func map() { 
    let 5km:CLLocationDistance = 5000 

    let a= "100 smith avenue some town 90210 SD" 
    let g = CLGeocoder() 

    g.geocodeAddressString(a) { [weak self] placemarks, error in 
     if let p = placemarks?.first, let l = placemark.location { 

      let p = MKPlacemark(coordinate: l.coordinate, addressDictionary: nil) 

      let cr = MKCoordinateRegionMakeWithDistance(l.coordinate, 5km, 5km) 
      let options = [ 
       MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: cr.center), 
       MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: cr.span) 
      ] 

      let m = MKMapItem(placemark: p) 
      m.name = "Your House" 
      m.openInMaps(launchOptions: options) 
     } 
    } 


} 
संबंधित मुद्दे