Search code examples
swiftgoogle-maps-sdk-ios

Swift: Google Map API Camera and MapView


With some online help, I successfully have implemented some Google map features in my iOS app.

extension GoogleMapViewController: GMSAutocompleteResultsViewControllerDelegate {
    func resultsController(resultsController: GMSAutocompleteResultsViewController,
                       didAutocompleteWithPlace place: GMSPlace) {
        searchController?.active = false
        // Do something with the selected place.
        print("Place name: ", place.name)
        print("Place address: ", place.formattedAddress)
        print("Place attributions: ", place.attributions)

        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.
        let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
        mapView.myLocationEnabled = true
        view = mapView

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = place.formattedAddress
        marker.map = mapView
    }
} 

I believe I have got the place.formattedAddress from search bar but how do I retrieve its coordinate so that I could set the camera and marker to show the searched place?


Solution

  • According to their documentation, you can get the location using place.coordinate

    let currentPlace: CLLocationCoordinate2D = place.coordinate
    

    Then you can get its latitude and longitude as currentPlace.latitude and currentPlace.longitude respectively

    Here's the documentation