有可能不接管屏幕吗?谷歌地方IOS迅速

时间:2016-07-29 23:14:50

标签: ios swift callback google-places-api gmsplacepicker

如果我从列表中选择一个像我附加的那个,那么Google下面的代码只返回一个地方。

我的问题: 是否有任何功能可以将所有地点的详细信息存储在给定的坐标中?例如,如果我的坐标为(51.5108396,-0.0922251),我如何获得附近地点的所有信息?我对Json不熟悉。有什么例子接近我想要的吗?非常感谢。

此函数placesClient.currentPlaceWithCallback在某种程度上接近我想要的但它不能使用自定义坐标,因为它使用用户的当前坐标。

//https://developers.google.com/places/ios-api/placepicker
let center = CLLocationCoordinate2DMake(51.5108396, -0.0922251)
let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
let config = GMSPlacePickerConfig(viewport: viewport)
let placePicker = GMSPlacePicker(config: config)
placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
    if let error = error {
        print("Pick Place error: \(error.localizedDescription)")
        return
    }
    if let place = place {
        print("Place name \(place.name)")
        print("Place address \(place.formattedAddress)")
        print("Place attributions \(place.attributions)")
    } else {
        print("No place selected")
    }
})

enter image description here

1 个答案:

答案 0 :(得分:0)

Fetching nearby places using google maps 由于升级的iOS版本,有些东西发生了变化。

完成更改的代码

func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius: Double, types:[String]) {
    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=\("your api key")&location=\(coordinate.latitude),\(coordinate.longitude)&radius=\(radius)&rankby=prominence&sensor=true"
    let typesString = types.count > 0 ? types.joinWithSeparator("|") : "food"
    urlString += "&types=\(typesString)"
    urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    let session = NSURLSession.sharedSession()
    let placesTask = session.dataTaskWithURL(NSURL(string: urlString)!) {data, response, error in
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        if let jsonResult = (try? NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as? NSDictionary {
            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
                                }
                            }
                        }
                        print("index", index, placeName, latitude, longitude)
                    }
                }
            }
        }
    }
    placesTask.resume()
}