0

地理位置情報ベースの値を取得するためにこのコードを試しましたが、都市名を取得できませんでした。都市名を取得するにはどうすればよいですか?

  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 {
    double lati = newLocation.coordinate.latitude;
   _geo_coder_latitude_lbl.text= [[NSString stringWithFormat:@"%f",lati] retain];
   _geocoder_latitude_str=_geo_coder_latitude_lbl.text;
   NSLog(@"print lat;%@",_geocoder_latitude_str);
   double longi = newLocation.coordinate.longitude;
   _geocoder_longitude_lbl.text= [[NSString stringWithFormat:@"%f",longi] retain];
   _geocoder_longitude_str=_geocoder_longitude_lbl.text;
   NSLog(@"print lat;%@",_geocoder_longitude_str);
  [self._geocoder reverseGeocodeLocation: locationManager.location completionHandler:
 ^(NSArray *placemarks, NSError *error)
 {

     //Get address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     NSLog(@"Placemark array: %@",placemark.addressDictionary );

     //String to address

     _located_address = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

     //Print the location in the console

     NSLog(@"Currently address is: %@",_located_address);

    // _ex_map_address_lbl.text=_located_address;

 }];

  [self _storeList_json_parser];

}
4

1 に答える 1

2

CLPlacemarkのドキュメントを参照してください[CLPlacemark locatity]。これは、目印に関連付けられている都市名を返します。

このサンプルコードを確認してください。

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
double lati = 45.46433;
double longi = 9.18839;

CLLocation *location = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(lati, longi) 
                                                     altitude:0 
                                           horizontalAccuracy:0
                                             verticalAccuracy:0 
                                                   timestamp:[NSDate date]];

[geocoder reverseGeocodeLocation:location completionHandler:
     ^(NSArray *placemarks, NSError *error)
     {
         CLPlacemark *placemark = [placemarks lastObject];
         if (error || !placemark)
             return;

         NSString *city = placemark.locality;
         if (!city)
             city = placemark.subAdministrativeArea;

         NSLog(@"City for location: %@", city);
}];
于 2012-11-21T08:51:27.647 に答える