2013-05-20 10 views
11

I aktuellen Standort Namen von Breiten- und Längengrad,Erhalten Ortsnamen von Breite und Länge in iOS

Hier ist mein Code-Schnipsel Ich habe versucht, finden wollen, aber mein Protokoll zeigt Null-Wert in allen Orten, außer in placemark, placemark.ISOcountryCode und placemark.country

Ich möchte Wert von placemark.locality und placemark.subLocality aber es zeigt Nullwerte.

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    // this creates the CCLocationManager that will find your current location 
    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    [locationManager startUpdatingLocation]; 

} 

// this delegate is called when the app successfully finds your current location 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
    [geocoder reverseGeocodeLocation:locationManager.location 
        completionHandler:^(NSArray *placemarks, NSError *error) { 
         NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!"); 

         if (error){ 
          NSLog(@"Geocode failed with error: %@", error); 
          return; 

         } 

         NSLog(@"placemarks=%@",[placemarks objectAtIndex:0]); 
         CLPlacemark *placemark = [placemarks objectAtIndex:0]; 

         NSLog(@"placemark.ISOcountryCode =%@",placemark.ISOcountryCode); 
         NSLog(@"placemark.country =%@",placemark.country); 
         NSLog(@"placemark.postalCode =%@",placemark.postalCode); 
         NSLog(@"placemark.administrativeArea =%@",placemark.administrativeArea); 
         NSLog(@"placemark.locality =%@",placemark.locality); 
         NSLog(@"placemark.subLocality =%@",placemark.subLocality); 
         NSLog(@"placemark.subThoroughfare =%@",placemark.subThoroughfare); 

        }]; 
} 

// this delegate method is called if an error occurs in locating your current location 
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); 
} 

Dank Im Voraus.

EDIT:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) 
     NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude); 

    // stop updating location in order to save battery power 
    [locationManager stopUpdatingLocation]; 


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

      // strAdd -> take bydefault value nil 
      NSString *strAdd = nil; 

      if ([placemark.subThoroughfare length] != 0) 
       strAdd = placemark.subThoroughfare; 

      if ([placemark.thoroughfare length] != 0) 
      { 
       // strAdd -> store value of current location 
       if ([strAdd length] != 0) 
        strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]]; 
       else 
       { 
        // strAdd -> store only this value,which is not null 
        strAdd = placemark.thoroughfare; 
       } 
      } 

      if ([placemark.postalCode length] != 0) 
      { 
       if ([strAdd length] != 0) 
        strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]]; 
       else 
        strAdd = placemark.postalCode; 
      } 

      if ([placemark.locality length] != 0) 
      { 
       if ([strAdd length] != 0) 
        strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]]; 
       else 
        strAdd = placemark.locality; 
      } 

      if ([placemark.administrativeArea length] != 0) 
      { 
       if ([strAdd length] != 0) 
        strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]]; 
       else 
        strAdd = placemark.administrativeArea; 
      } 

      if ([placemark.country length] != 0) 
      { 
       if ([strAdd length] != 0) 
        strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]]; 
       else 
        strAdd = placemark.country; 
      } 
     } 
    }]; 
} 
+2

Sie müssen eine API von Drittanbietern wie 'Google Places API' verwenden. –

+1

Sie müssen den kostenpflichtigen Google-Dienst nicht verwenden. Diese Funktionalität ist in iOS integriert. Sehen Sie sich CLLocation und CLPlacemark an. Es bietet Ihnen eine beliebige Granularität, die Sie von Coords erwarten, z. Land, Stadt, Region, Zeitzone, usw. etc. – samiles

Antwort

11

ich zu bewegen Ich gebe Ihnen einen Ausschnitt, den ich für die Auflösung der Adresse verwende. Ich schließe den Kommentar auch an der notwendigen Stelle ein, um den Code für Sie zu verstehen. Abgesehen davon, zögern Sie nicht, irgendeine Frage aus dem Snippet zu stellen, wenn Sie nichts verstehen.

schreiben folgenden Ausschnitt in didUpdateToLocation Methode

NSLog(@"didUpdateToLocation: %@", newLocation); 
CLLocation *currentLocation = newLocation; 

if (currentLocation != nil) 
    NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude); 

// stop updating location in order to save battery power 
[locationManager stopUpdatingLocation]; 


// Reverse Geocoding 
NSLog(@"Resolving the Address"); 

// “reverseGeocodeLocation” method to translate the locate data into a human-readable address. 

// The reason for using "completionHandler" ---- 
    // Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes. 

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

     // strAdd -> take bydefault value nil 
     NSString *strAdd = nil; 

     if ([placemark.subThoroughfare length] != 0) 
      strAdd = placemark.subThoroughfare; 

     if ([placemark.thoroughfare length] != 0) 
     { 
      // strAdd -> store value of current location 
      if ([strAdd length] != 0) 
       strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]]; 
      else 
      { 
      // strAdd -> store only this value,which is not null 
       strAdd = placemark.thoroughfare; 
      } 
     } 

     if ([placemark.postalCode length] != 0) 
     { 
      if ([strAdd length] != 0) 
       strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]]; 
      else 
       strAdd = placemark.postalCode; 
     } 

     if ([placemark.locality length] != 0) 
     { 
      if ([strAdd length] != 0) 
       strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]]; 
      else 
       strAdd = placemark.locality; 
     } 

     if ([placemark.administrativeArea length] != 0) 
     { 
      if ([strAdd length] != 0) 
       strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]]; 
      else 
       strAdd = placemark.administrativeArea; 
     } 

     if ([placemark.country length] != 0) 
     { 
      if ([strAdd length] != 0) 
       strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]]; 
      else 
       strAdd = placemark.country; 
     } 

Wo strAdd-Adresse Geolocation zurück ..

Genießen Programmierung !!

+0

' currentLocation' ist unbekannt – Krunal

+0

currentLocation ist newLocation von dieser Methode. Überprüfen Sie meine aktualisierte Antwort –

+0

Hey, ich habe alles für Sie getan. Sie müssen nur Ihren Code in ersetzen - (void) locationManager: (CLLocationManager *) Manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation mit mir, und Sie werden Ihre Antwort haben. –

0

Wenn der Ort, um Sie versuchen dann in dieser Liste aufgeführt ist es etwas falsch in Ihrem Code ist ..

http://developer.apple.com/library/ios/#technotes/tn2289/_index.html#//apple_ref/doc/uid/DTS40011305

Ansonsten hat CLGeoCoder keine Adressen in anderen Ländern.

Ich konfrontiert das gleiche Problem, ich habe dies verwendet, um die Adresse zu bekommen .. Gibt eine wirklich genaue Adresse.

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

0

Zu allererst Filterstellen in didUpdateToLocation Gebrauch gecached oder falschen Stellen für die Geocodierung

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

if (abs(locationAge) > 5.0) return; 

if (newLocation.horizontalAccuracy < 0) return; 

auch zu verhindern, versuchen Sie die Inverse Geokodierung Methode weg von didUpdateToLocation

+0

Apple Demo-Beispiel zeigt auch Null-Wert, warum ist es eine Idee? 'http: //developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html # // apple_ref/doc/uid/DTS40011097' – Krunal

0

Sobald Sie latitude haben, longitude Werte einer Position können Sie CLGeocoder verwenden hier ist ein tutorial, die Ihnen helfen könnten.

+0

Und es kann für iOS 5 und höher Versionen verwendet werden –

+0

Apple Demo-Beispiel zeigt auch null Wert, warum ist es eine Idee? 'http://developer.apple.com/library/ios/# samplecode/GeocoderDemo/Einführung/Intro.html # // apple_ref/doc/uid/DTS40011097' – Krunal

+0

Apple Demo-Beispiel zeigt auch Null-Wert, warum ist es eine Idee? 'http: //developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html # // apple_ref/doc/uid/DTS40011097' – Krunal

0

Verwenden Sie diese API zum Abrufen der Daten und zum Weitergeben von Längen- und Breitenwerten.

API for Geo location

+0

Apple-Demo-Beispiel zeigt auch null Wert, warum ist es eine Idee? 'http: //developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html # // apple_ref/doc/uid/DTS40011097' – Krunal

9

Methode mit Abschluss-Block:

typedef void(^addressCompletion)(NSString *); 

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletion)completionBlock 
{ 
    __block CLPlacemark* placemark; 
    __block NSString *address = nil; 

    CLGeocoder* geocoder = [CLGeocoder new]; 
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) 
    { 
     if (error == nil && [placemarks count] > 0) 
     { 
      placemark = [placemarks lastObject]; 
      address = [NSString stringWithFormat:@"%@, %@ %@", placemark.name, placemark.postalCode, placemark.locality]; 
      completionBlock(address); 
     } 
    }]; 
} 

Dies ist, wie es zu benutzen:

CLLocation* eventLocation = [[CLLocation alloc] initWithLatitude:_latitude longitude:_longitude]; 

[self getAddressFromLocation:eventLocation complationBlock:^(NSString * address) { 
     if(address) { 
      _address = address; 
     } 
    }]; 
0

Versuchen Sie, diese

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     //..NSLog(@"Found placemarks: %@, error: %@", placemarks, error); 
     if (error == nil && [placemarks count] > 0) { 
      placemark = [placemarks lastObject]; 
      lblgetaddrees.text = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@", 
            placemark.subThoroughfare, placemark.thoroughfare, 
            placemark.postalCode, placemark.locality, 
            placemark.administrativeArea, 
            placemark.country]; 
     } else { 
      //..NSLog(@"%@", error.debugDescription); 
     } 
    } ]; 
3

I Niru-Lösung in Swift 3, implementiert Posting hier sollte es jemand braucht:

let geocoder = CLGeocoder() 
geocoder.reverseGeocodeLocation(self.location!) { (placemarksArray, error) in 

    if (placemarksArray?.count)! > 0 { 

     let placemark = placemarksArray?.first 
     let number = placemark!.subThoroughfare 
     let bairro = placemark!.subLocality 
     let street = placemark!.thoroughfare 

     self.addressLabel.text = "\(street!), \(number!) - \(bairro!)" 
    } 
}