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 及更高版本中,您可以使用Map Kit FrameworkCLGeocoder的 Core Location 框架,对于低于 5.0 的 iOS 。MKReverseGeocoder祝你好运!

于 2012-09-26T10:13:36.167 回答
0

这是从当前位置获取地址字符串的块

in .h file

typedef void(^addressCompletionBlock)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock;

in .m file
#pragma mark - Get Address
-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletionBlock)completionBlock
{
    //Example URL
    //NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=23.033915,72.524267&sensor=true_or_false";

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true_or_false",location.coordinate.latitude, location.coordinate.longitude];

    NSError* error;
    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:&error];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[locationString dataUsingEncoding:NSUTF8StringEncoding]
                                                          options:0 error:NULL];

    NSString *strFormatedAddress = [[[jsonObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"formatted_address"];
    completionBlock(strFormatedAddress);
}

并调用函数

CLLocation* currentLocation = [[CLLocation alloc] initWithLatitude:[SharedObj.current_Lat floatValue] longitude:[SharedObj.current_Long floatValue]];

    [self getAddressFromLocation:currentLocation complationBlock:^(NSString * address) {
        if(address) {
            NSLog(@"Address:: %@",address);
        }
    }];
于 2016-04-13T13:07:55.217 回答