objective c how to get placemarks of given coordinates

落爺英雄遲暮 提交于 2019-12-23 11:53:26

问题


I have a question related to reverse geo-coding.

In my app, I have some coordinates (not my current coordinates) and I want to convert them into placemarks. I've dug a lot of websites and codes but they are all about reverse geocoding of current location...

Is there any way to get placemarks of specified coordinates (which are not current location)?

And if there is, please help me with some code or references.


回答1:


You can achieve this in two ways:-

First way:- Get the info using google api

-(void)findAddresstoCorrespondinglocation
{
    NSString *str = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=false",myCoordInfo.latitude,myCoordInfo.longitude];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
    [request setRequestMethod:@"GET"];
    [request setDelegate:self];
    [request setDidFinishSelector: @selector(mapAddressResponse:)];
    [request setDidFailSelector: @selector(mapAddressResponseFailed:)];
    [networkQueue addOperation: request];
    [networkQueue go];

}

in response you will get all information about the location coordinates you specified.

Second Approach:-

Implement reverse geocoding

a.)add mapkit framework

b.)Make instance of MKReverseGeocoder in .h file

MKReverseGeocoder *reverseGeocoder;

c.)in .m file

self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:cordInfo];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];

Implement two delegate methods of MKReverseGeoCoder

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

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    NSString *city = myPlacemark.thoroughfare;
    NSString *subThrough=myPlacemark.subThoroughfare;
    NSString *locality=myPlacemark.locality;
    NSString *subLocality=myPlacemark.subLocality;
    NSString *adminisArea=myPlacemark.administrativeArea;
    NSString *subAdminArea=myPlacemark.subAdministrativeArea;
    NSString *postalCode=myPlacemark.postalCode;
    NSString *country=myPlacemark.country;
    NSString *countryCode=myPlacemark.countryCode;
    NSLog(@"city%@",city);
    NSLog(@"subThrough%@",subThrough);
    NSLog(@"locality%@",locality);
    NSLog(@"subLocality%@",subLocality);
    NSLog(@"adminisArea%@",adminisArea);
    NSLog(@"subAdminArea%@",subAdminArea);
    NSLog(@"postalCode%@",postalCode);
    NSLog(@"country%@",country);
    NSLog(@"countryCode%@",countryCode);

    }


来源:https://stackoverflow.com/questions/9031373/objective-c-how-to-get-placemarks-of-given-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!