How to get lat and long coordinates from address string
Asked Answered
M

4

9

I have a MKMapView that has a UISearchBar on the top, and I want the user to be able to type a address, and to find that address and drop a pin on it. What I don't know is how to turn the address string into longitude and latitude, so I can make a CLLocation object. Does anyone know how I can do this?

Mose answered 1/9, 2013 at 21:21 Comment(1)
This doesn't help, because the data he retrieves has the coordinates for the addresses. I need a way to find the coordinates for the address, when the user searches for the address.Mose
D
18

You may find your answer in this question.

iOS - MKMapView place annotation by using address instead of lat / long By User Romes.

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];
                 }
             }
         ];

A Very simple solution. But only applicable on iOS5.1 or later.

Diplocardiac answered 1/8, 2014 at 10:10 Comment(0)
A
3

I used a similar approach like Vijay, but had to adjust one line of code. region.center = placemark.region.center didn't work for me. Maybe my code helps someone as well:

let location: String = "1 Infinite Loop, CA, USA"
let geocoder: CLGeocoder = CLGeocoder()
geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
    if (placemarks?.count > 0) {
        let topResult: CLPlacemark = (placemarks?[0])!
        let placemark: MKPlacemark = MKPlacemark(placemark: topResult)
        var region: MKCoordinateRegion = self.mapView.region

        region.center.latitude = (placemark.location?.coordinate.latitude)!
        region.center.longitude = (placemark.location?.coordinate.longitude)!

        region.span = MKCoordinateSpanMake(0.5, 0.5)

        self.mapView.setRegion(region, animated: true)
        self.mapView.addAnnotation(placemark)
    }
}) 
Angora answered 12/3, 2016 at 20:59 Comment(0)
S
1

For swift2

var location: String = "some address, state, and zip"
        var geocoder: CLGeocoder = CLGeocoder()
        geocoder.geocodeAddressString(location,completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in
            if (placemarks?.count > 0) {
                var topResult: CLPlacemark = (placemarks?[0])!
                var placemark: MKPlacemark = MKPlacemark(placemark: topResult)
                var region: MKCoordinateRegion = self.mapView.region
                region.center = placemark.region.center
                region.span.longitudeDelta /= 8.0
                region.span.latitudeDelta /= 8.0
                self.mapView.setRegion(region, animated: true)
                self.mapView.addAnnotation(placemark)
            }
        })
Scoff answered 16/12, 2015 at 5:26 Comment(0)
P
0
func geoCodeUsingAddress(address: NSString) -> CLLocationCoordinate2D {
    var latitude: Double = 0
    var longitude: Double = 0
    let addressstr : NSString = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=\(address)" as NSString
    let urlStr  = addressstr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    let searchURL: NSURL = NSURL(string: urlStr! as String)!
    do {
        let newdata = try Data(contentsOf: searchURL as URL)
        if let responseDictionary = try JSONSerialization.jsonObject(with: newdata, options: []) as? NSDictionary {
            print(responseDictionary)
            let array = responseDictionary.object(forKey: "results") as! NSArray
            let dic = array[0] as! NSDictionary
            let locationDic = (dic.object(forKey: "geometry") as! NSDictionary).object(forKey: "location") as! NSDictionary
            latitude = locationDic.object(forKey: "lat") as! Double
            longitude = locationDic.object(forKey: "lng") as! Double
        } catch {
        }
    }
    var center = CLLocationCoordinate2D()
    center.latitude = latitude
    center.longitude = longitude
    return center
}
Puga answered 31/3, 2017 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.