2016-05-16 12 views
0

CLLocationManagerDelegateを使用して正常に動作していたボタンのクリックでジオコードを逆にしました。しかし今、私はUISearchBarを使ってユーザーから名前を取得し、ボタンをクリックすると、ユーザーが入力した場所とマーカーが表示されます。 ジオコーディングに時間がかかるので、場所に値は保存されませんが、アプリはその作業を終了します。 誰も私にそれをする方法を教えてもらえますか?どこか間違っていると私を訂正してください。単一アプリケーションでの順方向ジオコーディングと逆方向ジオコーディング:必要な方向

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


    } 

答えて

0

私は方法とその作業を今見つけました。私が厄介な方法でそれをやっているなら、私を訂正してください。 誰かがそれをしたいと思ったら答えはここにあります:

#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_; 
       } 
      }]; 

     } 

    }]; 


} 
関連する問題