如何将地理坐标转换为准确的地址?

时间:2016-08-30 13:42:27

标签: ios swift

我想将用户的位置(包含纬度和经度值)转换为准确的地址。但它不起作用,它在执行后返回一个空字符串。

另外,Xcode建议我使用CNPostalAddress,我不知道它是什么。

这是功能

{
  "project": {
    "version": "1.0.0-beta.11-webpack.3",
    "name": "demo"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets",
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css",
        "../node_modules/bootstrap/dist/css/bootstrap.min.css"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "dev": "environments/environment.dev.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false,
    "lazyRoutePrefix": "+"
  }
}

这是警告 Warnnings

如果你能帮助我解决它,请表示感谢:)

3 个答案:

答案 0 :(得分:1)

import AddressBook替换为import Contacts,并替换

带有addressDictionary.objectForKey(kABPersonAddressStreetKey)

addressDictionary.objectForKey(CNPostalAddress.Street) 带有addressDictionary.objectForKey(kABPersonAddressStateKey)

addressDictionary.objectForKey(CNPostalAddress.State) 带有addressDictionary.objectForKey(kABPersonAddressCityKey)

addressDictionary.objectForKey(CNPostalAddress.City)

您可以在Apple Douments

中获取更多信息

使用完成

func reverseGeocoder (completion: (info:String) -> Void){

    var geocoder = CLGeocoder()
    var currentUserLocation = CLLocation(latitude: 30, longitude: 122)
    geocoder.reverseGeocodeLocation(currentUserLocation, completionHandler: {
        (placemarks,error) -> Void in
        if placemarks != nil && placemarks?.count > 0{
            let placemark = placemarks![0] as CLPlacemark
            let addressDictionary = placemark.addressDictionary! as NSDictionary

            let str:NSMutableString = ""
            if let address = addressDictionary.objectForKey(kABPersonAddressStreetKey) as? String{
                str.appendString(address)
            }

            if let state = addressDictionary.objectForKey(kABPersonAddressStateKey) as? String{
                str.appendString(state)
            }

            if let city = addressDictionary.objectForKey(kABPersonAddressCityKey) as? String{
                str.appendString(city)
            }
            completion(info: str as String)

        }
    }
    )

答案 1 :(得分:0)

所以它看起来像iOS 9中的新类。你必须导入contancts。

import Contacts

CNPostalAddress封装了城市,街道,州等信息。 这是关于它的更多细节。 https://developer.apple.com/library/ios/documentation/Contacts/Reference/CNPostalAddress_Class/

答案 2 :(得分:0)

if let coordinates = base.coOrdinates
        {
          if let lat = coordinates.lat {
            let location = CLLocation(latitude: lat, longitude: coordinates.lon!)
            let reverseGeocoder = CLGeocoder()
            reverseGeocoder.reverseGeocodeLocation(location, completionHandler: {place,_ in
              //print(place)
              let myPlacemark = place![0]
              let countryName = myPlacemark.country
              let cityName = myPlacemark.subAdministrativeArea
              let streetName = myPlacemark.subLocality
              NSOperationQueue.mainQueue().addOperationWithBlock {
                self.lastLocationLabel.text = "\(streetName!), \(cityName!), \(countryName!)"
              }
            })
          } else {
            self.lastLocationLabel.text = "Not available"
          }
        }

Thsi可能会帮到你。这就是我如何从坐标获取位置字符串。