2012-06-19 18 views
-1

我在iphone上得到了latitute和longitute。但我想找到基於該Latitute和Longitute的地址。我使用Ios5。請給我源代碼,找出Address.I使用此代碼 .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"); 
} 

但它給人messege-錯誤。

+0

使用谷歌的地理編碼API http://maps.googleapis.com/maps/api/geocode/ json?latlng = 40.714224,-73.961452&sensor = true,並解析json或xml with lat long,如果您要使用 –

回答

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 
+0

,請確保添加NSXMLParserDelegate – superGokuN

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 
    } 
}]; 
相關問題