Google maps iOS SDK place id on tap

2019-06-24 05:22发布

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条回答
Emotional °昔
2楼-- · 2019-06-24 05:56

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.

查看更多
登录 后发表回答