Pru*_*ran 6 google-maps google-places-api google-maps-sdk-ios swift ios8
我正在尝试使用谷歌地图来查找我当前位置的附近地点.我在Google Developer Console中创建了一个项目,并获得了"ios APIkey"和"Server APIkey".我还启用了Google Places API和适用于iOS的Google Maps SDK.以下是靠近地方查找的代码.
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, name : String){
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\(apiServerKey)&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
urlString += "&name=\(name)"
urlString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
println(urlString)
if placesTask.taskIdentifier > 0 && placesTask.state == .Running {
placesTask.cancel()
}
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
println("inside.")
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
if let json = NSJSONSerialization.JSONObjectWithData(data, options:nil, error:nil) as? NSDictionary {
if let results = json["results"] as? NSArray {
for rawPlace:AnyObject in results {
println(rawPlace)
self.results.append(rawPlace as! String)
}
}
}
self.placesTask.resume()
}
}
Run Code Online (Sandbox Code Playgroud)
传递的坐标是当前位置的坐标.如果我执行上面的代码,没有任何事情发生,但生成的url是有效的.如果我把这个网址放在Google上,我会得到正确的结果.但我的应用程序中没有任何内容 请帮我解决这个问题.请告诉我哪里错了!
zta*_*tan 10
您可以将地点添加到结果数组中,但是您已经做了任何更新mapview的操作,例如,向mapview添加标记.
示例代码将标记添加到mapview:
let returnedPlaces: NSArray? = jsonResult["results"] as? NSArray
if returnedPlaces != nil {
for index in 0..<returnedPlaces!.count {
if let returnedPlace = returnedPlaces?[index] as? NSDictionary {
var placeName = ""
var latitude = 0.0
var longitude = 0.0
if let name = returnedPlace["name"] as? NSString {
placeName = name as String
}
if let geometry = returnedPlace["geometry"] as? NSDictionary {
if let location = geometry["location"] as? NSDictionary {
if let lat = location["lat"] as? Double {
latitude = lat
}
if let lng = location["lng"] as? Double {
longitude = lng
}
}
}
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(latitude, longitude)
marker.title = placeName
marker.map = self.mapView
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在Swift中看到有关如何使用Google地图获取附近地点的教程.
| 归档时间: |
|
| 查看次数: |
12078 次 |
| 最近记录: |