0

如果我从列表中选择一个(如我附加的那个),下面来自 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")
    }
})

在此处输入图像描述

4

1 回答 1

0

使用谷歌地图获取附近的地方 由于升级了 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()
}
于 2016-07-30T03:34:03.077 回答