Google maps iOS SDK place id on tap

元气小坏坏 提交于 2019-12-23 12:31:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!