-1

我在 iphone 中得到纬度和经度。但我想根据该纬度和经度查找地址。我用过ios5。请给我源代码以查找地址。我使用了这个代码 .m 文件

MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:Coordinate2D];

[geocoder setDelegate:self];

[geocoder start]; // put in vievdidload


-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{    
    NSLog(@"The geocoder has returned: %@", [placemark country]);
}

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"Error");
}

但它给出了消息错误。

4

2 回答 2

0

一种方法是使用 google api 获取位置名称并解析 xml。希望对你有帮助

//Place below parser code where you are reading latlng and place your latlng in the url

NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false"]];
[parser setDelegate:self];
[parser parse];

// Below are the delegates which will get you the exact address easyly

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{    
  if([elementName isEqualToString:@"formatted_address"]){
    got = YES; //got is a BOOL
  }
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
  if(got){
    NSLog(@"the address is = %@",string);
  }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

}

//what we are doing is using xmlparser to parse the data which we get through the google map api copy above link and use in browser you will see the xml data brought
于 2012-06-19T06:53:51.967 回答
0

如果您使用的是 iOS5,那么这里是代码:-

CLGeocoder *geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithLatitude:10.0 longitude:10.0];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  if(!error){
       CLPlacemark *placemark = [placemarks objectAtIndex:0];
       MKPlacemark *placemark1 = [[MKPlacemark alloc]initWithPlacemark:placemark];
       [self.mapView addAnnotation:placemark1];   //mapView is object of MKMapView
     }
}];
于 2012-06-19T09:16:27.847 回答