2017-02-24 5 views
0

Ich bekomme Breitengrad und umgekehrte Geokodierung in IOS. Das habe ich bisher gemacht.umgekehrte Geocodierung gibt falsche Adresse zurück

-(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); 
    } 
} ]; 

}

Aber mein Problem ist in Android und iOS gibt 2 verschiedene Adressen für diese. Aber die Android Adresse ist korrekt. So erhält die Android-App die Adresse.

{ 
     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

Reverse-Geocoding

[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); 
    } 
} ]; 

Diese currentLocation ich auf diese Weise ich wonna eine Vermutung machen

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

Antwort

0

bekam !!

Was sind diese lati und longi? Sollten Sie nicht longitude und longitude anstelle

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

Statt:

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

Ich denke, hasan auf etwas ist. Sie verwenden die Variablen 'lati' und' longi', die an keiner Stelle im von Ihnen geposteten Code definiert sind. Sie müssen Instanzvariablen von anderswo sein. Sie konvertieren auch Ihre Breiten- und Längengrade in Zeichenfolgen. Sie sollten sie wirklich als Doppel speichern und die doppelten Werte direkt verwenden. –

+0

@DuncanC, genau die String-Sache auch. Hab das nicht gesehen! Wenn du nicht willst, füge eine Antwort hinzu. Bitte erlauben Sie mir, Ihren Punkt zu meinem hinzuzufügen. – hasan83

+1

Auf jeden Fall. Du warst "am Anfang" mit der scheinbar passenden Antwort, also solltest du derjenige sein. –

Verwandte Themen