ios - 没有从纬度和经度获得正确的地址

标签 ios swift geolocation

我想从纬度和经度中获取真实的邮政地址,但是效果不佳。也许我注意到使用了正确的 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")
            }
        })

但是我没有得到真实的地址。例如,如果那个经纬度地址是

东街123号

我得到的东西只有东街(没有号码)或靠近公路。

我需要做什么才能获得真实地址?

最佳答案

我不确定这是否是您要找的,但我目前使用它来获得一个很好的可读地址。我还将这些值发送到我的应用程序委托(delegate),以便稍后在需要时引用。我没有用 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);
        }
    } ];
}

关于ios - 没有从纬度和经度获得正确的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35243120/

相关文章:

iOS 在 UITextView 中的单词后放置光标

ios - SwiftUI - 如何关闭工作 TableView ,同时关闭该 View

ios - UIButton 不响应嵌套在另一个 UIScrollView 中的 UIScrollView 中的触摸

iphone - MPMoviePlayerController 立即结束

ios - 在 swift 中为标签设置尾部缩进

java - 我正在实例化从 Java (J2ObjC) 转译的 Swift 非 ARC Objective-C 类,它是否泄漏安全?

ios - Cocoapods 正在安装旧的 Pod 版本

objective-c - 关于 CLLocationManager startMonitoringForRegion 的 iOS 应用程序生命周期

android - PhoneGap - Android 上的 Cordova 2.1 地理定位不一致

javascript - 无法在 React 中返回带有地理位置坐标的 JSX