使用谷歌地图提取附近的地方 [英] Fetching nearby places using google maps

查看:191
本文介绍了使用谷歌地图提取附近的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Google地图来查找我当前位置的附近地点。
我在Google Developer Console中创建了一个项目,并获得了'ios APIkey'和'Server APIkey'。我还为iOS启用了Google Places API和Google Maps SDK。
以下是按地点查找的代码。

  func fetchPlacesNearCoordinate(坐标: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 ==。运行{
placesTask.cancel()
$ b}
UIApplication.sharedApplication()。networkActivityIndi​​catorVisible = true
placesTask = session.dataTaskWithURL(NSURL (string:urlString)!){data,response,error in
println(inside。)
UIApplication.sharedApplication()。networkActivityIndi​​catorVisible = false
如果让json = NSJSONSerialization.JSONObjectWithData(数据,选项:无,错误:无)为? NSDictionary {
如果让结果= json [results] as? NSArray {
for rawPlace:结果中的AnyObject {
println(rawPlace)
self.results.append(rawPlace as!字符串)
}
}
}
self.placesTask.resume()
}
}

传递的坐标是当前位置的坐标。如果我执行上面的代码,没有任何事情发生,但生成的网址是有效的。如果我把这个网址在Google中得到正确的结果。但在我的应用程序中没有显示。请帮我解决这个问题。并请让我知道哪里错了!

解决方案

您将场所添加到结果数组中,但是您已经完成了更新mapview的任何操作,例如,添加标记

示例代码添加标记到mapview:

  let返回的地方:NSArray? = jsonResult [results] as? NSArray 

如果returnedPlaces!= nil {

for index in 0 ..< returnedPlaces!.count {

如果让returnedPlace = returnedPlaces? [指数]为? NSDictionary {

var placeName =
var latitude = 0.0
var longitude = 0.0

如果让name = returnedPlace [name] as ? NSString {
placeName = name as String
}

如果let geometry = returnedPlace [geometry] as? NSDictionary {
如果让位置=几何[位置]为? NSDictionary {
如果let lat = location [lat] as? Double {
latitude = lat
}

如果让lng = location [lng]为? Double {
longitude = lng
}
}
}

让marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(纬度,经度)
marker.title = placeName
marker.map = self.mapView
}
}
}

您可以看到本教程,介绍如何使用Swift中的Google Maps获取附近的地方。

Am trying to implement to find nearby places of my current location using google maps. I have created a project in Google Developer Console and got 'ios APIkey' and 'Server APIkey'. I also enabled Google Places API and Google Maps SDK for iOS. Below is the code to find out near by places.

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()
    }
}

The passed coordinate is current location's coordinate. If I execute a above code, nothing is happening but generated url is valid one. If i put that url in Google am getting correct results. But nothing displaying in my app. Please help me to resolve this. and please let me know where am wrong!!!

解决方案

You add your places to the results array, but you have done anything to update your mapview, for example, adding markers to your mapview.

sample code add marker to 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
                }
           }
   }

You can see this tutorial about how to get nearby places with Google Maps in Swift.

这篇关于使用谷歌地图提取附近的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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