24

座標値から場所名を取得したい。これがコードです、

- (void)viewWillAppear:(BOOL)animated {  
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 39.281516;
    zoomLocation.longitude= -76.580806;
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];                
    [_mapView setRegion:adjustedRegion animated:YES];      
}

だから、その緯度と経度から、私はその場所の名前を知りたいです。

4

5 に答える 5

81

以下のコードは ios5 以降で動作します。

 CLGeocoder *ceo = [[CLGeocoder alloc]init];
 CLLocation *loc = [[CLLocation alloc]initWithLatitude:32.00 longitude:21.322]; //insert your coordinates

[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              if (placemark) {


                  NSLog(@"placemark %@",placemark);
                  //String to hold address
                  NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                  NSLog(@"addressDictionary %@", placemark.addressDictionary);

                  NSLog(@"placemark %@",placemark.region);
                  NSLog(@"placemark %@",placemark.country);  // Give Country Name
                  NSLog(@"placemark %@",placemark.locality); // Extract the city name
                  NSLog(@"location %@",placemark.name);
                  NSLog(@"location %@",placemark.ocean);
                  NSLog(@"location %@",placemark.postalCode);
                  NSLog(@"location %@",placemark.subLocality);

                  NSLog(@"location %@",placemark.location);
                  //Print the location to console
                  NSLog(@"I am currently at %@",locatedAt);
              }
              else {
                  NSLog(@"Could not locate");
              }
          }
 ];
于 2012-09-26T10:19:40.213 に答える
7
-(NSString *)getAddressFromLatLon:(double)pdblLatitude withLongitude:(double)pdblLongitude
{
  NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=csv",pdblLatitude, pdblLongitude];
  NSError* error;
  NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
   // NSLog(@"%@",locationString);

  locationString = [locationString stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  return [locationString substringFromIndex:6];
}
于 2012-10-08T10:39:49.283 に答える
3

この方法を使用する

CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 

[geocoder reverseGeocodeLocation: zoomLocation completionHandler:^(NSArray* placemarks, NSError* error){
}
 ];
于 2012-09-26T10:18:41.470 に答える
1

iOS 5.0 以降でCLGeocoderは Core Location フレームワークを、iOS 5.0 未満でMKReverseGeocoderは Map Kit Framework を使用できます。幸運を!

于 2012-09-26T10:13:36.167 に答える