2016-05-16 10 views
0

Ich habe die Geocodierung bei einem Klick auf die Schaltfläche umgekehrt, was mit CLLocationManagerDelegate funktioniert. Aber jetzt nahm ich eine UISearchBar, um Namen vom Benutzer und auf Knopfklick zu machen, ich möchte den vom Benutzer eingegebenen Platz und eine Markierung darauf zeigen. Da die Geocodierung einige Zeit dauert, wird kein Wert im Standort gespeichert, aber die App beendet ihre Arbeit. Also kann mir jemand sagen, wie es geht? und korrigiere mich bitte, wenn ich irgendwo falsch liege.Vorwärts- und Rückwärts-Geocodierung in einer einzelnen App: Need Direction

- (IBAction)searchLocation:(id)sender { 

     NSString *address = self.searchBar.text; 
     NSLog(@"%@",address); 

     [forwardGeocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { 

      if ([placemarks count]>0) { 

       CLPlacemark *placeMarkForward = [placemarks objectAtIndex:0]; 

       locationValue = placeMarkForward.location; 
       NSLog(@"Location : :%@",locationValue); 

      } 
     }]; 

     [reverseGeocoder reverseGeocodeLocation:locationValue completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { 

      if (error == nil && [placemarks count]>0) { 
       CLPlacemark *placeMarkReverse = [placemarks lastObject]; 
       CLLocation *currentLocation; 
       marker.position = CLLocationCoordinate2DMake(currentLocation.coordinate.latitude,currentLocation.coordinate.longitude); 
       marker.title = [NSString stringWithFormat:@"%@",placeMarkReverse.country]; 

       marker.snippet = [NSString stringWithFormat:@"%@\n%@", placeMarkReverse.locality,placeMarkReverse.postalCode]; 
       marker.map = mapView_; 
      } 
     }]; 


    } 

Antwort

0

Ich fand einen Weg und seine Arbeit jetzt. Korrigiere mich, wenn ich es auf unordentliche Weise mache. Hier ist die Antwort, wenn jemand es tun möchte:

#pragma mark - Serach Location - 
- (IBAction)searchLocation:(id)sender { 

    NSString *address = self.searchBar.text; 
    NSLog(@"%@",address); 



    [forwardGeocoder geocodeAddressString:address completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { 

     if ([placemarks count]>0) { 

      CLPlacemark *placeMarkForward = [placemarks objectAtIndex:0]; 

      locationValue = placeMarkForward.location; 
      NSLog(@"Location : :%@",locationValue); 

      [reverseGeocoder reverseGeocodeLocation:locationValue completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) { 

       if (error == nil && [placemarks count]>0) { 
        CLPlacemark *placeMarkReverse = [placemarks lastObject]; 

        NSLog(@"Testing"); 
        [mapView_ animateToLocation:CLLocationCoordinate2DMake(locationValue.coordinate.latitude,locationValue.coordinate.longitude)]; 
        marker.position = CLLocationCoordinate2DMake(locationValue.coordinate.latitude,locationValue.coordinate.longitude); 
        marker.title = [NSString stringWithFormat:@"%@",placeMarkReverse.locality]; 

        marker.snippet = [NSString stringWithFormat:@"%@\n%@", placeMarkReverse.postalCode,placeMarkReverse.country]; 
        marker.map = mapView_; 
       } 
      }]; 

     } 

    }]; 


} 
Verwandte Themen