问题
Is it possible to get the place id when tapping on a POI (iOS google maps SDK).
For example clicking on a restaurant I would like to bring up some of the information such as reviews and opening times.
Looking at the docs I can see the delegate method mapView(mapView: GMSMapView!, didTapAtCoordinate coordinate: CLLocationCoordinate2D)
but nothing that provides the place id. Im not sure how accurate it would be to try and find the exact place id using just the coordinates.
回答1:
You can do a Google Places API request to the place ID. You can do the API request by using a coordinate.
Sample API request URL:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&key=API_KEY
In iOS, you can do an API request by NSURLSession
:
func placesAPITest(coordinate: CLLocationCoordinate2D) {
let requestURL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(coordinate.latitude),\(coordinate.longitude)&radius=500&key=YOUR_API_KEY"
let request = NSURLRequest(URL: NSURL(string:requestURL)!)
let session = NSURLSession.sharedSession()
session.dataTaskWithRequest(request,
completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) in
if error == nil {
let object = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as! NSDictionary
println(object)
let results = object["results"] as! [NSDictionary]
for place in results {
println(place["place_id"])
println(place["opening_hours"])
}
}
else {
println("Places API error")
}
}).resume()
}
You can get a place ID from the JSON response.
来源:https://stackoverflow.com/questions/30709367/google-maps-ios-sdk-place-id-on-tap