AAA
AAA

Reputation: 95

How to get Coordinates from Google Places API in ios Swift

I am using Alamofire and SwiftyJSON to auto complete Google Places. I integrated it well and good like this :

enter image description here

Now, what I need is if I select a place from the table view, it is selected in the text field. But how can I get the respective latitude and longitude of that place, so that I can add annotation of that place in MapKit

Is it legal to get details from Google Place API and display them on apple's MapKit? I got this doubt after seeing Viking's question

Upvotes: 5

Views: 9223

Answers (3)

Mohammad Takbiri
Mohammad Takbiri

Reputation: 39

I know it's too late for the answer, But hope to be useful for the others. Actually you have to define your required fields for the output.

    let autocompleteController = GMSAutocompleteViewController()
    autocompleteController.delegate = self

    // Specify the place data types to return.
    let fields: GMSPlaceField = GMSPlaceField(rawValue: UInt(GMSPlaceField.name.rawValue) |
        UInt(GMSPlaceField.placeID.rawValue) |
        UInt(GMSPlaceField.coordinate.rawValue))
    autocompleteController.placeFields = fields

    // Specify a filter.
    let filter = GMSAutocompleteFilter()
    filter.countries = ["AE"]
    autocompleteController.autocompleteFilter = filter

    // Display the autocomplete view controller.
    present(autocompleteController, animated: true, completion: nil)

Upvotes: 1

nikhil nangia
nikhil nangia

Reputation: 581

Maybe i m bit late but stil as a tuttorial i m posting this answer you can simply get the coordinates like this

// Handle the user's selection 

public func viewController(viewController: GMSAutocompleteViewController, didAutocompleteWithPlace place: GMSPlace) {
    var lat = place.coordinate.latitude
    var lon = place.coordinate.longitude
    print("lat lon",lat,lon)

}

Upvotes: 13

tskulbru
tskulbru

Reputation: 2346

You can get the coordinates from the GMSPlace object. See the documentation.

Regarding if you can use google places alongside apple map, no you can't. As stated in their Terms of Use

Make sure that any maps used by your app to show the location of places retrieved from the Google Places API for iOS are Google maps.

Upvotes: 2

Related Questions