我无法获得州和国家CLGeocoder的邮政编码。我得到位置名称,国家和地区。我只是在印度使用这个代码来获取位置。我正在使用下面的代码。
func getAddressFromLocation(location: CLLocation) {
CLGeocoder().reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
print(location)
self.activityIndicator.stopAnimating()
if error != nil {
DILog.print(items: "Reverse geocoder failed with error" + (error.debugDescription))
return
}
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
if placeMark == nil {
return
}
// Location name
if let locationName = placeMark.addressDictionary!["Name"] as? String {
self.currentLocation = self.currentLocation + locationName
}
// Street address
if let street = placeMark.addressDictionary!["Thoroughfare"] as? String {
self.currentLocation = self.currentLocation + ", " + street
self.spotLocation.street_address_1 = street
}
// City
if let city = placeMark.addressDictionary!["City"] as? String {
print(city, terminator: "")
self.currentLocation = self.currentLocation + ", " + city
self.spotLocation.city = city
}
// Country
if let country = placeMark.addressDictionary!["Country"] as? String {
print(country, terminator: "")
self.currentLocation = self.currentLocation + ", " + country
self.spotLocation.country = country
}
// Street address
if let street1 = placeMark.addressDictionary!["SubThoroughfare"] as? String {
self.currentLocation = self.currentLocation + ", " + street1
self.spotLocation.street_address_1 = street1
}
// state
if let state = placeMark.addressDictionary!["State"] as? String {
self.currentLocation = self.currentLocation + ", " + state
self.spotLocation.state = state
}
// Zip
if let zip = placeMark.addressDictionary!["Zip"] as? String {
self.currentLocation = self.currentLocation + ", " + zip
self.spotLocation.state = zip
}
self.locationLabel.text? = self.currentLocation
})
}
请告诉我如何才能获得解决方案?
答案 0 :(得分:0)
let zipCode = placeMark.postalCode ?? "unknown"