Prerequisites
First we need to get the users location by getting their current longitude and latitude.
import GooglePlaces
import GooglePlacePicker
CLLOcationManagerDelegate
protocolclass ViewController: UIViewController, CLLocationManagerDelegate {
}
var currentLocation = CLLocationManager()
currentLocation = CLLocationManager()
currentLocation.requetAlwayAuthorization()
@IBAction func placePickerAction(sender: AnyObject) {
if CLLOcationManager.authorizationStatues() == .AuthorizedAlways {
let center = CLLocationCoordinate2DMake((currentLocation.location?.coordinate.latitude)!, (currentLocation.location?.coordinate.longitude)!)
let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
placePicker = GMSPlacePicker(config: config)
placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
if let error = error {
print("Pick Place error: \(error.localizedDescription)")
return
}
if let place = place {
print("Place name: \(place.name)")
print("Address: \(place.formattedAddress)")
} else {
print("Place name: nil")
print("Address: nil")
}
})
}
}