【问题标题】:How to get lat and long coordinates from address string如何从地址字符串中获取经纬度坐标
【发布时间】:2013-09-04 23:08:56
【问题描述】:

我有一个MKMapView,顶部有一个UISearchBar,我希望用户能够输入地址,找到该地址并在其上放置一个图钉。我不知道的是如何将地址字符串变成经纬度,所以我可以制作一个CLLocation对象。有谁知道我该怎么做?

【问题讨论】:

  • 这没有帮助,因为他检索到的数据有地址的坐标。当用户搜索地址时,我需要一种方法来查找地址的坐标。

标签: mapkit cllocation cllocationcoordinate2d


【解决方案1】:

您可以在这个问题中找到答案。

iOS - MKMapView place annotation by using address instead of lat / long 用户 Romes。

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

一个非常简单的解决方案。但仅适用于 iOS5.1 或更高版本。

【讨论】:

    【解决方案2】:

    我使用了类似 Vijay 的方法,但必须调整一行代码。 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)
        }
    }) 
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:
        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
        }
        

        【讨论】:

          猜你喜欢
          • 2011-06-14
          • 2021-05-13
          • 2021-06-27
          • 2013-07-04
          • 1970-01-01
          • 2010-12-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多