-1

iPhoneで緯度と経度を取得します。しかし、その緯度と経度に基づいて住所を見つけたいと思っています。Ios5を使用しています。Find Address のソースコードを教えてください。このコード .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

1 つの方法は、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 に答える