views:

128

answers:

2

Hi, is there a way to make the MKMapView place a pin with a given address? Without using the Coordinates

Thanks

A: 

You need to use a geocoding service to convert your address to coordinates. Google, I think, offers one, along with a few other services.

Jasconius
+1  A: 

here is a snippet of code I used in an application

-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
    NSString *urlStr = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
                           [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
    NSArray *items = [locationStr componentsSeparatedByString:@","];

    double lat = 0.0;
    double long = 0.0;

    if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:@"200"]) {
        lat = [[items objectAtIndex:2] doubleValue];
        long = [[items objectAtIndex:3] doubleValue];
    }
    else {
        NSLog(@"Address, %@ not found: Error %@",addressStr, [items objectAtIndex:0]);
    }
    CLLocationCoordinate2D location;
    location.latitude = lat;
    location.longitude = long;

    return location;
}
Aaron Saunders
It's worth checking out the Google geocoding terms and conditions (http://code.google.com/apis/maps/documentation/geocoding/), particularly if you are building a commercial application.
Colin Pickard