Google会在iOS上轻松映射iOS SDK位置ID [英] Google maps iOS SDK place id on tap

查看:102
本文介绍了Google会在iOS上轻松映射iOS SDK位置ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在POI(iOS google maps SDK)上点击时获取地点ID。

例如,点击一家餐厅,我想调出一些信息,如评论和开放时间。



查看文档可以看到委托方法 mapView(mapView:GMSMapView !, didTapAtCoordinate coordinate:CLLocationCoordinate2D)但没有提供地点ID。我不确定使用坐标来查找确切的位置id有多准确。

你可以做一个将 Google Places API请求添加到地点ID。您可以使用坐标来执行API请求。

示例API请求网址:

  https:// maps。 googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&key=API_KEY 

在iOS中,您可以通过 NSURLSession 执行API请求:

  func placesAPITest(coordinate:CLLocationCoordinate2D){

let requestURL =https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\\ \\(坐标。海拔),\(coordinate.longitude)& radius = 500& key = YOUR_API_KEY
let request = NSURLRequest(URL:NSURL(string:requestURL)!)
let session =



$ b session.dataTaskWithRequest(request,
completionHandler:{(data:NSData!,response:NSURLResponse!,error:NSError!)if error = = nil {
let object = NSJSONSerialization.JSONObjectWithData(data,options:nil,错误:无)为! NSDictionary
println(object)

let results = object [results] as!
println(place [place_id])
println(place [opening_hours])
}
}
$ {b $ b println(Places API error)
}

})。resume()
}

您可以从 JSON响应


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.

解决方案

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.

这篇关于Google会在iOS上轻松映射iOS SDK位置ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆