iOS - MKMapView place annotation by using address instead of lat / long iOS - MKMapView place annotation by using address instead of lat / long ios ios

iOS - MKMapView place annotation by using address instead of lat / long


Based on psoft's excellent information, I was able to achieve what I was looking for with this code.

NSString *location = @"some address, state, and zip";CLGeocoder *geocoder = [[CLGeocoder alloc] init];            [geocoder geocodeAddressString:location                  completionHandler:^(NSArray* placemarks, NSError* error){                     if (placemarks && placemarks.count > 0) {                         CLPlacemark *topResult = [placemarks objectAtIndex:0];                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];                         MKCoordinateRegion region = self.mapView.region;                         region.center = placemark.region.center;                         region.span.longitudeDelta /= 8.0;                         region.span.latitudeDelta /= 8.0;                         [self.mapView setRegion:region animated:YES];                         [self.mapView addAnnotation:placemark];                     }                 }             ];


Refactored Swift version:

let location = "some address, state, and zip"let geocoder = CLGeocoder()geocoder.geocodeAddressString(location) { [weak self] placemarks, error in    if let placemark = placemarks?.first, let location = placemark.location {        let mark = MKPlacemark(placemark: placemark)        if var region = self?.mapView.region {            region.center = location.coordinate            region.span.longitudeDelta /= 8.0            region.span.latitudeDelta /= 8.0            self?.mapView.setRegion(region, animated: true)            self?.mapView.addAnnotation(mark)        }    }}


What you're after is called geocoding or forward-geocoding. Reverse-geocoding is the process of converting a lat/long pair to street address.

iOS5 provides the CLGeocoder class for geocoding. MKPlacemark supports reverse-goecoding in iOS >= 3.0. The data involved of course is very large, so your app will generally need network access to the functionality.

A good place to start is Apple's Location Awareness Programming Guide. Also, there are LOTS of questions about this here on SO. https://stackoverflow.com/search?q=geocoding

Good luck!