-1

I want to search for a destination using the name in MapKit and get back the longitude and latitude (CLLocationCoordinate2D).

Currently I'm using the hardcoded value to set the destination.

    // Make a directions request
    MKDirectionsRequest *directionsRequest = [MKDirectionsRequest new];

    // Start at our current location
    MKMapItem *source = [MKMapItem mapItemForCurrentLocation];
    [directionsRequest setSource:source];

    // Make the destination --> I WANT TO GET THIS COORDINATE USING NAME
    CLLocationCoordinate2D destinationCoords = CLLocationCoordinate2DMake(45.545824, 9.327515);
    MKPlacemark *destinationPlacemark = [[MKPlacemark alloc] initWithCoordinate:destinationCoords addressDictionary:nil];
    MKMapItem *destination = [[MKMapItem alloc] initWithPlacemark:destinationPlacemark];
    [directionsRequest setDestination:destination];
user1872384
  • 6,886
  • 11
  • 61
  • 103
  • 1
    Check this : http://stackoverflow.com/questions/18563084/how-to-get-lat-and-long-coordinates-from-address-string – Bista Apr 28 '15 at 04:29

1 Answers1

1

You have to use CLGeocoder to forward-geocode using an address:

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"New York City" completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {
        CLPlacemark *placemark = [placemarks lastObject];
        MKCoordinateRegion region;
        region.center.latitude = placemark.location.coordinate.latitude;
        region.center.longitude = placemark.location.coordinate.longitude;
        [self.mapView setRegion:region animated:YES];
    }
}];

This will set the map's region to the location returned, using the latitude and longitude as it's centre.

John Rogers
  • 2,192
  • 19
  • 29
  • Thx, John Rogers. Got it. However the search results are not as robust as the one offered by google... Some search keywords will return empty placemarks. – user1872384 Apr 28 '15 at 05:46
  • Sam Vermette created a geocoder that uses Google's Geocoding API. Check it out here: https://github.com/TransitApp/SVGeocoder – John Rogers Apr 28 '15 at 05:47