有没有办法使用 google 获取 mkmapview 的方向(只是路线)?

Is there a way to get directions(just routes) using google for mkmapview?

我在 iOS 上使用 swift 并使用 MKMapView。 我一直致力于为用户提供一个从 - 到文本字段,并让用户在从和到位置之间有一种路线形式。我已经使用内置的方向和地理编码 api 调用 Apple 来处理 mkmapview。但是用了一段时间后发现苹果不支持很多国家Apple - Supported Countries For Directions

那么有没有办法从 google 地图 SDK 获取路线并将它们转换为 MKPolyline 或 MKOverlay?我没有使用过 google 地图,所以请记住这一点。我看到这个问题 iPhone: Using Google to get directions 它有答案,但最新的解决方案已有 4 年历史,并且在 objective-C 中。尽管我确实了解如何将 objective-C 代码转换为 swift,但我宁愿不必完成整个项目。请记住,这是一个更大项目的一部分,所以我无法将整个项目切换到 google 地图。

我认为this Whosebug answer可以将编码折线转换为MKPolyline。

答案是Objective-C版本,所以我尝试将其转换为Swift,示例代码:

func polyLineWithEncodedString(encodedString: String) -> MKPolyline {
        let bytes = (encodedString as NSString).UTF8String
        let length = encodedString.lengthOfBytesUsingEncoding(NSUTF8StringEncoding)
        var idx: Int = 0

        var count = length / 4
        var coords = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(count)
        var coordIdx: Int = 0

        var latitude: Double = 0
        var longitude: Double = 0

        while (idx < length) {
            var byte = 0
            var res = 0
            var shift = 0

            do {
                byte = bytes[idx++] - 0x3F
                res |= (byte & 0x1F) << shift
                shift += 5
            } while (byte >= 0x20)

            let deltaLat = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
            latitude += Double(deltaLat)

            shift = 0
            res = 0

            do {
                byte = bytes[idx++] - 0x3F
                res |= (byte & 0x1F) << shift
                shift += 5
            } while (byte >= 0x20)

            let deltaLon = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
            longitude += Double(deltaLon)

            let finalLat: Double = latitude * 1E-5
            let finalLon: Double = longitude * 1E-5

            let coord = CLLocationCoordinate2DMake(finalLat, finalLon)
            coords[coordIdx++] = coord

            if coordIdx == count {
                let newCount = count + 10
                let temp = coords
                coords.dealloc(count)
                coords = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(newCount)
                for index in 0..<count {
                    coords[index] = temp[index]
                }
                temp.destroy()
                count = newCount
            }

        }

        let polyLine = MKPolyline(coordinates: coords, count: coordIdx)
        coords.destroy()

        return polyLine
    }

您可以尝试 this GitHub link 中的示例项目。

下面是使用 Google Direction API Web 服务在 MKMapView 上呈现从旧金山到圣何塞的路线的图像。

使用google api查找出发地和目的地之间的路线。

所需参数:

  • origin : lat + long 当前位置或地点 ID .

  • 目的地:lat + long 目的地位置或地点 ID .

  • 密钥:创建新的google api密钥。

可选参数:

  • mode (defaults to driving) 驾驶给你更多选择,或者设置
    步行、骑自行车、乘坐公交车。
  • 备选方案:false 只给你一条路线,option true 给出起点和目的地之间的所有方向,然后你可以选择哪个 路线你想要持续时间或腿内距离。

然后使用 Polyline 库将点(在 overview_polyline 内)转换为 CLLocationCoordinate2D 列表 https://github.com/raphaelmor/Polyline

像这样:

func convertPointToCoordinates() -> [CLLocationCoordinate2D]? {

let polyline = Polyline(encodedPolyline: overviewPolyline.points ?? "")

return polyline.coordinates

}

let coordinates = convertPointToCoordinates()

let polyLine = MKPolyline(coordinates: coordinates ?? [], count: (coordinates?.count ?? 0))

  • 在地图视图上添加折线:

    self.mapView.add(polyLine)

  • 关注折线区域:

    self.mapView.setVisibleMapRect(polyLine.boundingMapRect, edgePadding: UIEdgeInsets.init(top: 60, left: 60, bottom: 60, right: 60), animated: true)

Swift 3版本,略有压缩,感谢ztan.

如果您想在 iOS 中使用 Googles 地理折线而不使用 Google SDK。

func polyLineWithEncodedString(encodedString: String) -> [CLLocationCoordinate2D] {
    var myRoutePoints=[CLLocationCoordinate2D]()
    let bytes = (encodedString as NSString).utf8String
    var idx: Int = 0
    var latitude: Double = 0
    var longitude: Double = 0
    while (idx < encodedString.lengthOfBytes(using: String.Encoding.utf8)) {
        var byte = 0
        var res = 0
        var shift = 0
        repeat {
            byte = bytes![idx] - 63
            idx += 1
            res |= (byte & 0x1F) << shift
            shift += 5
        } while (byte >= 0x20)
        let deltaLat = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
        latitude += Double(deltaLat)

        shift = 0
        res = 0
        repeat {
            byte = bytes![idx] - 63
            idx += 1
            res |= (byte & 0x1F) << shift
            shift += 5
        } while (byte >= 0x20)
        let deltaLon = ((res & 1) != 0x0 ? ~(res >> 1) : (res >> 1))
        longitude += Double(deltaLon)

        myRoutePoints.append(CLLocation(latitude: Double(latitude * 1E-5), longitude: Double(longitude * 1E-5)).coordinate)
    }
    return myRoutePoints
}