2013-09-01 6 views
7

मेरे पास MKMapView है जिसमें शीर्ष पर UISearchBar है, और मैं चाहता हूं कि उपयोगकर्ता कोई पता टाइप कर सके, और उस पते को ढूंढने और उस पर एक पिन ड्रॉप करने के लिए। मुझे नहीं पता कि पता स्ट्रिंग को देशांतर और अक्षांश में कैसे चालू किया जाए, इसलिए मैं CLLocation ऑब्जेक्ट बना सकता हूं। क्या कोई जानता है मैं ऐसा कैसे कर सकता हूँ?पता स्ट्रिंग से lat और long coordinates कैसे प्राप्त करें

+0

यह मदद नहीं करता है, डेटा वह पुन: प्राप्त करता पतों के लिए निर्देशांक नहीं रही हैं। जब उपयोगकर्ता पते की खोज करता है, तो मुझे पते के निर्देशांक खोजने का एक तरीका चाहिए। –

उत्तर

16

आपको इस प्रश्न में आपका उत्तर मिल सकता है।

iOS - MKMapView place annotation by using address instead of lat/long उपयोगकर्ता रोम द्वारा।

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

एक बहुत ही सरल समाधान। लेकिन केवल आईओएस 5.1 या बाद में लागू होता है।

1

swift2

var location: String = "some address, state, and zip" 
     var geocoder: CLGeocoder = CLGeocoder() 
     geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in 
      if (placemarks?.count > 0) { 
       var topResult: CLPlacemark = (placemarks?[0])! 
       var placemark: MKPlacemark = MKPlacemark(placemark: topResult) 
       var region: MKCoordinateRegion = self.mapView.region 
       region.center = placemark.region.center 
       region.span.longitudeDelta /= 8.0 
       region.span.latitudeDelta /= 8.0 
       self.mapView.setRegion(region, animated: true) 
       self.mapView.addAnnotation(placemark) 
      } 
     }) 
2

के लिए मैं विजय की तरह एक समान दृष्टिकोण का इस्तेमाल किया है, लेकिन कोड की एक पंक्ति को समायोजित करने के लिए किया था। region.center = placemark.region.center मेरे लिए काम नहीं किया। शायद मेरे कोड के रूप में अच्छी तरह से किसी को मदद करता है:

let location: String = "1 Infinite Loop, CA, USA" 
let geocoder: CLGeocoder = CLGeocoder() 
geocoder.geocodeAddressString(location,completionHandler: {(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.mapView.region 

     region.center.latitude = (placemark.location?.coordinate.latitude)! 
     region.center.longitude = (placemark.location?.coordinate.longitude)! 

     region.span = MKCoordinateSpanMake(0.5, 0.5) 

     self.mapView.setRegion(region, animated: true) 
     self.mapView.addAnnotation(placemark) 
    } 
}) 
0
func geoCodeUsingAddress(address: NSString) -> CLLocationCoordinate2D { 
    var latitude: Double = 0 
    var longitude: Double = 0 
    let addressstr : NSString = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(address)" as NSString 
    let urlStr = addressstr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 
    let searchURL: NSURL = NSURL(string: urlStr! as String)! 
    do { 
     let newdata = try Data(contentsOf: searchURL as URL) 
     if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary { 
      print(responseDictionary) 
      let array = responseDictionary.object(forKey: "results") as! NSArray 
      let dic = array[0] as! NSDictionary 
      let locationDic = (dic.object(forKey: "geometry") as! NSDictionary).object(forKey: "location") as! NSDictionary 
      latitude = locationDic.object(forKey: "lat") as! Double 
      longitude = locationDic.object(forKey: "lng") as! Double 
     } catch { 
     } 
    } 
    var center = CLLocationCoordinate2D() 
    center.latitude = latitude 
    center.longitude = longitude 
    return center 
} 
+0

यह Google मानचित्र के लिए है (नवीनतम स्विफ्ट में जवाब 3) –

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