ios - 使用 GoogleMaps SDK 生成路线?

标签 ios swift google-maps-sdk-ios

我是新手,正在努力使用 Google Maps sdk。是否可以使用 google maps sdk 实际生成路线?就像我可以制作一个显示“生成路线”的按钮,然后一旦点击它就会生成一条用户可以实际导航的路线,就像他们使用真正的谷歌地图应用程序一样?谢谢。

最佳答案

例子:

func getDiectionFromGoogle(startCoordinate: CLLocationCoordinate2D, toLocation: CLLocationCoordinate2D) {
    var urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(startCoordinate.latitude),\(startCoordinate.longitude)&destination=\(toLocation.latitude),\(toLocation.longitude)&key=\("your key!!")"
    urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
    let session = URLSession.shared
    let placesTask = session.dataTask(with: URL(string: urlString)!, completionHandler: {data, response, error in
        if error != nil {
        }
        if let jsonResult = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary {

            // *******UPDATE**********
            print(jsonResult) // this returns a dictionary

            if let routes = jsonResult["routes"] as? NSArray {
                // example to get data from dictionary
            }

        })
        placesTask.resume()
    }

看看你在 jsonResult 中得到了什么。您可以找到为您的方向生成路径所需的所有坐标等等!

此外,您还可以在 json 请求中使用更多参数。看看这里。 https://developers.google.com/maps/documentation/directions/intro

//10 月 15 日更新

我猜你遇到了编码路径的问题。这是我解码路径的方式。我很确定我没有做最简单的方法,但我只是忽略它,因为它确实有效。哈哈

// you can see a key called polyline in somewhere in the dictionary
// polylineCoordinates are all the coordinates for your routes
// path can be used for polyline
if let polyline = (paths[i] as AnyObject).value(forKey: "polyline") as? NSDictionary {
    if let points = polyline.value(forKey: "points") as? String {
        let gmsPath = GMSPath(fromEncodedPath: points)
        for index in 0 ..< gmsPath!.count() {
            let coordinate = gmsPath!.coordinate(at: index)
            self.polylineCoordinates.append(coordinate)
            self.path.add(coordinate)
        }
    }
}

这是一份关于如何使用折线的 Google 文档。我不会给你任何代码。我很确定你能解决它。边做边学?但如果你坚持某件事,你可以回到我身边。我会尽力帮助:) https://developers.google.com/maps/documentation/ios-sdk/shapes

我不确定 Google 是否提供类似于 Google map 的导航。我自己构建应用程序的导航。如果可以的话,我可以尽力帮助你。

//10 月 20 日更新

你快到了。 path[i] 来自这里 LOL。非常难看的代码。

if let jsonResult = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary {
    if let routes = jsonResult["routes"] as? NSArray {
        if let legs = routes.value(forKey: "legs") as? NSArray {
            if let steps = legs.value(forKey: "steps") as? NSArray {
                if steps.count != 0 {
                    if let firstStep = steps.object(at: 0) as? NSArray {
                        if firstStep.count != 0 {
                            if let paths = firstStep.object(at: 0) as? NSArray {
                                if paths.count > 0 {
                                    for i in 0 ..< paths.count {

enter image description here

要查看我的图表,path[0] 是 intersection1 的起始坐标,path 1是intersection1到intersection2,path[2]是intersection2到结束坐标。

其实我的self.path是没有用的,你可以直接从jsonResult中解码出path。我之所以使用 self.polylineCoordinates 是因为我想获取所有坐标(如我图中的红色三角形)来创建我自己的导航。在 jsonResult 中,你可以看到一些名为 startAt(如果我没记错的话)和 endAt 的键,它们是坐标,在我的图中表示为黑色三角形。您可能需要获取所有坐标的原因是因为从路口到另一个路口的路径可能不是直线,它可以是曲线,如 intersection1 到 intersection2。

关于ios - 使用 GoogleMaps SDK 生成路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39649676/

相关文章:

ios - Swift NSURL 在有效网址上给出错误

ios - 如何使 Obj-C 库全局可重命名(每个类/导出的符号)?

ios - 在应用程序委托(delegate)中全局更改色调后临时更改色调

ios - UISplitViewController 首先显示详细 View

swift - FloatingPoint 扩展 - 使用其他 FloatingPoint 类型初始化

swift - 通过在 map 上滑动来拖动 GoogleMaps-Marker

ios - 谷歌地图 sdk ios Api 错误

ios - Swift 中 View 透明度导致的深色背景问题

iphone - Xcode 谷歌地图搜索栏

swift - 如何知道闭包是否归类所有?