Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot addresses in Swift, converting address to longitude and latitude coordinates?

In Objective-C this code works fine to plot an address and it finds the longitude and latitude coordinates:

     NSString *address = @"1 Infinite Loop, CA, USA";
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address 
             completionHandler:^(NSArray* placemarks, NSError* error){
                 // Check for returned placemarks
                 if (placemarks && placemarks.count > 0) {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     // Create a MLPlacemark and add it to the map view
                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
                     [self.mapView addAnnotation:placemark];
                     [placemark release];
                 }
                 [geocoder release];
             }];

I have been searching for days on how to do this with Swift?! Could anyone point me in the right direction? Could you explain if a user needs an internet connection for this to work. Sorry if these questions sounds a bit basic, but I am quite new to this and learning every day.

I understand that I have to use this code, but cannot find a way to implement it

func geocodeAddressString(_ addressString: String!,
    completionHandler completionHandler: CLGeocodeCompletionHandler!)
like image 374
agf119105 Avatar asked Jul 11 '14 21:07

agf119105


People also ask

How do I find the latitude and longitude of an address in Swift?

geocodeAddressString(addressString) { (placemarks, error) in if error == nil { if let placemark = placemarks?[0] { let location = placemark. location! completionHandler(location. coordinate, nil) return } } completionHandler(kCLLocationCoordinate2DInvalid, error as NSError?) } }

Which method is used to get an address for a given latitude and longitude?

Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA") into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map.


2 Answers

XCode 9 & Swift 3:

import CoreLocation

let address = "1 Infinite Loop, CA, USA"
let geocoder = CLGeocoder()

geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
   if((error) != nil){
      print("Error", error)
   }
   if let placemark = placemarks?.first {
      let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate
      }
    })
like image 133
realtimez Avatar answered Oct 13 '22 12:10

realtimez


Try something like

var address = "1 Infinite Loop, CA, USA"
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address, {(placemarks: [AnyObject]!, error: NSError!) -> Void in
    if let placemark = placemarks?[0] as? CLPlacemark {
        self.mapView.addAnnotation(MKPlacemark(placemark: placemark))
    }
})
like image 22
kviksilver Avatar answered Oct 13 '22 11:10

kviksilver