2017-02-24 58 views
0

我得到緯度經度並在ios中做反向地理編碼。這是我迄今爲止所做的。反向地理編碼返回錯誤的地址

-(void)GetLocationData 
{ 
if (self.locationManager == nil) 
{ 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate = self; 
} 
else 
{ 
    nil; 
} 

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) 
{ 
    [self.locationManager requestWhenInUseAuthorization]; 
} 
else 
{ 
    nil; 
} 

self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;//kCLLocationAccuracyBest; 
[self.locationManager startUpdatingLocation]; 
} 



- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
NSLog(@"didUpdateToLocation: %@", newLocation); 
CLLocation *currentLocation = newLocation; 
NSString *longitude; 
NSString *latitude; 

if (currentLocation != nil) { 
    longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];// 
    latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];// 

    NSLog(@"Longitude-----%@-----Latitude----%@",longitude,latitude); 
    dm.strLatitude=latitude; 
    dm.strLongitude=longitude; 
} 

[self.locationManager stopUpdatingLocation]; 



geocoder = [[CLGeocoder alloc] init]; 
// Reverse Geocoding 
NSLog(@"Resolving the Address"); 
CLLocationDegrees degreesLati=lati; 
CLLocationDegrees degreesLongi=longi; 
CLLocation *loc=[[CLLocation alloc] initWithLatitude:degreesLati longitude:degreesLongi]; 
[geocoder reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error) { 
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
    if (error == nil && [placemarks count] > 0) { 
     placemark = [placemarks objectAtIndex:[placemarks count]-1]; 

     NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
          placemark.subThoroughfare, placemark.thoroughfare, 
          placemark.postalCode, placemark.subLocality, 
          placemark.subAdministrativeArea, 
          placemark.country]; 

     // NSString *address=[self.placemark]; 

     NSDictionary *dictAddress = [NSDictionary dictionaryWithDictionary:placemark.addressDictionary]; 
     NSMutableDictionary *dictTxtData = [NSMutableDictionary dictionary]; 
     NSLog(@"country:%@",dictAddress); 
     strCountry=placemark.country; 

     NSLog(@"Address------%@",address); 
    } else { 
     NSLog(@"%@", error.debugDescription); 
    } 
} ]; 

}

但我的問題是,在Android和iOS返回2個不同的地址本。但Android地址是正確的。這是Android應用獲取地址的方式。

{ 
     Geocoder geocoder = new Geocoder(context, Locale.ENGLISH); 

     try { 
      /** 
      * Geocoder.getFromLocation - Returns an array of Addresses 
      * that are known to describe the area immediately surrounding the given latitude and longitude. 
      */ 
      List<Address> addresses = geocoder.getFromLocation(latitude, longitude, this.geocoderMaxResults); 

      return addresses; 
      } catch (IOException e) { 
      //e.printStackTrace(); 
      Log.e(TAG, "Impossible to connect to Geocoder", e); 
     } 

} 

UPDATE

反向地理編碼

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
    if (error == nil && [placemarks count] > 0) { 

     placemark = [placemarks objectAtIndex:[placemarks count]-1]; 

     NSString *address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@", 
          placemark.subThoroughfare, placemark.thoroughfare, 
          placemark.postalCode, placemark.subLocality, 
          placemark.subAdministrativeArea, 
          placemark.country]; 

     // NSString *address=[self.placemark]; 

     NSDictionary *dictAddress = [NSDictionary dictionaryWithDictionary:placemark.addressDictionary]; 
     NSMutableDictionary *dictTxtData = [NSMutableDictionary dictionary]; 



     strCountry=placemark.country; 

     NSLog(@"Address------%@",address); 
    } else { 
     NSLog(@"%@", error.debugDescription); 
    } 
} ]; 

currentLocation我這樣

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
     CLLocation *currentLocation = newLocation; 

回答

0

我wonna做出猜測了!

那些什麼是latilongi?你不應該使用longitudelongitude代替

CLLocationDegrees degreesLati = currentLocation.coordinate.longitude; 
CLLocationDegrees degreesLongi = currentLocation.coordinate.latitude; 

相反的:

CLLocationDegrees degreesLati=lati; 
CLLocationDegrees degreesLongi=longi; 
+1

我認爲哈桑是要發生什麼。你使用變量'lati'和'longi',這些變量在你發佈的代碼中沒有定義。它們必須是其他地方的實例變量。您還可以將經緯度轉換爲字符串。您應該將它們保存爲雙精度,並直接使用雙精度值。 –

+0

@DuncanC,也是字符串的東西。沒看見!如果你不想添加答案。請允許我將你的觀點加入我的觀點。 – hasan83

+1

通過一切手段。你是「第一次走出去」,看起來是什麼答案,所以你應該是一個。 –