我想从Latitude&中获取真实的邮政地址。然而,经度运作不佳。也许我注意到使用正确的API等。
例如,我有以下代码
var location = CLLocation(latitude:currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
print(location)
if error != nil {
print("Reverse geocoder failed with error" + error!.localizedDescription)
return
}
if placemarks!.count > 0 {
let pm = placemarks![0] as! CLPlacemark
street = String(pm.thoroughfare!)
city = String(pm.locality!)
state = String(pm.administrativeArea!)
print(pm.locality)
print(pm.administrativeArea)
print(pm.thoroughfare)
self.utility.setMyLocation(dName, longitude: long, latitude: lat, street: street, city: city, state: state)
locManager.stopUpdatingLocation()
}
else {
print("Problem with the data received from geocoder")
}
})
但是我没有得到真实的地址。例如,假设该long和lat地址是
东街123号
我得到的东西只有东街(没有号码)或靠近公路。
为了获得真实的地址,我需要做什么?
答案 0 :(得分:1)
我不确定这是否是你想要的,但我目前用来获得一个很好的可读地址。我还将这些值发送给我的应用程序委托,以便稍后在需要时进行引用。我没有用Swift写的,只是在Objective C中。:/但我觉得关键是reverseGeocodeLocation
#pragma mark - location Manager update and FUNCTIONS
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%s Failed with Error: %@", __PRETTY_FUNCTION__, error);
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(@"%s Update to Location: %@", __PRETTY_FUNCTION__, newLocation);
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"Longitude: %@", longitude);
NSLog(@"Latitude: %@", latitude);
// Set current values to the AppDelegate for refrencing
appDelegate.locationDetailLon = longitude;
appDelegate.locationDetailLat = latitude;
}
// Stop Location Manager to save power
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Translating and getting the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
// NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
NSLog(@"Address:\n%@", address);
// Set current values to the AppDelegate for refrencing
appDelegate.locationDetailState = placemark.administrativeArea;
appDelegate.locationDetailCountry = placemark.country;
appDelegate.locationDetailCity = placemark.locality;
appDelegate.locationDetailPostCode = placemark.postalCode;
} else {
NSLog(@"%s ERROR: %@", __PRETTY_FUNCTION__, error.debugDescription);
}
} ];
}