开发者

Xcode iOS 4.2.1 -> Give Map View the address to show the pin? [duplicate]

This question already has an answer here: Closed 10 years ago.

Possible Duplicate:

Forward Geocode Example using CLGeocoder

I have a plist full of names and their adrresses. I want my iOS a开发者_如何学JAVApp to show on a map view a pin with the address of the person he has selected. Can I import the address to map view and get a pin? And how?? Thank you.


Use the CLGeocoder class to convert addresses into lat/long coordinates:

Forward Geocode Example using CLGeocoder

The more accurate address you have, the better the result should be so with exact addresses you should get very accurate points.


Another option is to use Google's geocoding web service, simply pass the address string to this function and you will get an CLLocationCoordinate2D which contains a latitude & longitude.

Right now it grabs the location at the 0th index, which is the closest matching result. Log out the result to see the JSON response from Google.

- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
    NSString *esc_addr =  [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *req = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?sensor=true&address=%@", esc_addr];
    CLLocationCoordinate2D center;


    NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
    if (result)
    {
        //NSLog(@"LOC RESULT: %@", result);
        NSError *e;
        NSDictionary *resultDict = [NSJSONSerialization JSONObjectWithData: [result dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options: NSJSONReadingMutableContainers
                                                                     error: &e];
        NSArray *resultsArray = [resultDict objectForKey:@"results"];

        if(resultsArray.count > 0)
        {
            resultDict = [[resultsArray objectAtIndex:0] objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
            center.latitude = [[resultDict objectForKey:@"lat"] floatValue];
            center.longitude = [[resultDict objectForKey:@"lng"] floatValue];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Results Found" message:@"No locations were found using, please try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
            [alert show];
        }

        //goes through each result
        /*for(NSDictionary *dict in resultsArray)
        {
            resultDict = [dict objectForKey:@"geometry"];
            resultDict = [resultDict objectForKey:@"location"];
        }*/
    }
    return center;
}


This is whats known as Forward Geocoding, and there is no built in API to do this for you. You need to use an external service, which one you use really depends on your app.

I have used Google's service for this. http://code.google.com/apis/maps/documentation/geocoding/

It is a very simple API, but has restrictions based on your app's license. I know there are a couple other services for this, but I'll leave that as an exercise to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜