2013-05-20 10 views
11

に緯度&経度からロケーション名を取得し、ここで私は緯度と経度から現在の場所の名前を見つけたいiOSの

は私のコードスニペットである私が試したが、私のログはplacemarkを除くすべての場所でnull値を示し、placemark.ISOcountryCodeそしてplacemark.country

placemark.localityplacemark.subLocalityの値が必要ですが、null値を示しています。

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

ありがとうございました。

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

'Google Places API'のようなサードパーティAPIを使用する必要があります。 –

+1

Googleの有料サービスを使用する必要はありません。この機能はiOSに組み込まれています。 CLLocationとCLPlacemarkを見てください。それはあなたがcoordsから望む任意の粒度レベルを提供します。国、都市、地域、タイムゾーンなど – samiles

答えて

11

からreverseGeoCoding方法を移動しようと、また

NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow]; 

if (abs(locationAge) > 5.0) return; 

if (newLocation.horizontalAccuracy < 0) return; 

をジオコーディングするために使用するキャッシュされたか、間違った場所を防ぐために私はあなたがアドレスを解決するために使用しているスニペットを与えます。私はあなたのためのコードを理解するために必要な場所にもコメントを入れています。あなたが何かを理解することに失敗する場合、それ以外にスニペットから任意の質問をすること自由に感じる。 didUpdateToLocation方法STRADDは、ジオロケーションを使用してアドレスを返します

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; 
     } 

書き込み次のスニペット...

プログラミングお楽しみください!!

+0

' currentLocation'は不明です – Krunal

+0

currentLocationはこのメソッドのnewLocationです。私の更新された答えを確認してください –

+0

ねえ、私はあなたのためにすべてをやった。 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation :(CLLocation *)oldLocationを使用してコードを置き換えるだけで、あなたの答えが得られます。 –

0

あなたが取得しようとしている場所が、このリストに記載されている場合は、あなたのコード内で何かが間違っ..です

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

さもなければCLGeoCoderは他の国へのアドレスを持っていません。

私は同じ問題に直面したので、私はこれを使ってアドレスを取得しました。本当に正確なアドレスを与えます。すべての

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

0

まず、didUpdateToLocationのフィルタの位置I「が離れdidUpdateToLocation

+0

リンゴのデモサンプルもnull値を示しています、なぜそれがいいですか? 'http://developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html#// apple_ref/doc/uid/DTS40011097' – Krunal

0

あなたはlatitudeを持っていたら、あなたはここにCLGeocoder を使用することができる場所のlongitude値はあなたを助けるかもしれないtutorialです。

+0

iOS 5以降のバージョンに使用できます –

+0

appleデモサンプルもnull値を示しています、なぜそれがいいですか? 'http://developer.apple。com/library/ios /#samplecode/GeocoderDemo/Introduction/Intro.html#// apple_ref/doc/uid/DTS40011097' – Krunal

+0

Appleデモサンプルもnull値を示しています。 'http://developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html#// apple_ref/doc/uid/DTS40011097' – Krunal

0

このAPIを使用してデータを取得し、緯度と経度の値を渡します。

API for Geo location

+0

リンゴのデモサンプルもnull値を示しています、それはどういう考えですか? 'http://developer.apple.com/library/ios/#samplecode/GeocoderDemo/Introduction/Intro.html#// apple_ref/doc/uid/DTS40011097' – Krunal

9

完了ブロックと方法:

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

これは、それを使用する方法である:

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

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

この

[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

を試してみて、私は誰もがそれを必要とする必要があり、ここで掲載、スウィフト3でNiruのソリューションを実装:

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!)" 
    } 
} 
関連する問題