簡體   English   中英

無法從“經度”獲得正確的地址

[英]Not getting a correct address from Latitude & Longitude

我想從緯度和經度中獲取真實的郵政地址,但是效果不佳。 也許我注意到使用了正確的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號

我得到的東西只有東街(沒有電話號碼)或附近的道路。

我需要做什么才能獲得真實地址?

我不確定這是否是您要尋找的東西,但是我目前使用它來獲取一個可讀的地址。 我還將這些值發送給我的應用程序委托,以便以后在需要時進行引用。 我沒有用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);
        }
    } ];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM