【问题标题】:Is it possible to not take over screen? Google Places IOS swift是否可以不接管屏幕? Google Places IOS swift
【发布时间】:2016-07-31 06:13:23
【问题描述】:

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

【问题讨论】:

  • 根本不清楚你在问什么。 Google 有多种 API 可用于获取有关位置的信息。有些与用户交互,还有一些后端 API 可让您进行查询并返回结果(如果有记忆,则以 JSON 格式)。你需要定义你需要做什么,然后找到一个 API 可以让你做到这一点。
  • 刚刚更新了我的问题。有Json的例子吗?
  • 可能,是的。但是,我不会为你做你的工作。我必须像你一样搜索 Google 的文档。
  • stackoverflow.com/questions/29636718/… 这是学习 JSON 并接近我想要的东西的好例子吗?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多