有没有办法使用谷歌获取方向(只是路线)mkmapview? [英] Is there a way to get directions(just routes) using google for mkmapview?

查看:236
本文介绍了有没有办法使用谷歌获取方向(只是路线)mkmapview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS上使用swift并使用MKMapView。
我一直致力于为用户提供从 - 到文本字段并让用户在到达和到达位置之间具有一种形式的路由。我已经使用内置的指示和地理编码api调用Apple的mkmapview。然而,在使用它一段时间后,意识到很多国家不被Apple支持 Apple支持的国家/地区

I'm using swift on iOS and using MKMapView. I've been working on giving a user a from - to textfield and letting the user have a form of route between the from and to locations. I've got that working on mkmapview using the built in directions and geocoding api calls for Apple. However after using it for a while a realized that a lot of countries are not supported by Apple Apple - Supported Countries For Directions

那么有没有办法从google maps SDK获取路线并将它们转换为MKPolyline或MKOverlay?我没有使用谷歌地图,所以记住这一点。我看到这个问题 iPhone:使用Google获取路线它有一个答案,但最新的解决方案是4岁,它的目标是C。虽然我知道如何将Objective-C代码转换为swift,但我宁愿不必经历整个项目。请记住,这是一个更大的项目的一部分,所以我不能将整个事情切换到谷歌地图。

So is there a way to get routes from the google maps SDK and translate them to a MKPolyline or MKOverlay? I haven't worked with google maps so keep that in mind. I saw this question iPhone: Using Google to get directions it has an answer, but the newest solution is 4 years old and its in objective-C. Although I do understand how to convert objective-C code to swift, I'd rather not have to go through a whole project. Keep in mind that this a part of a bigger project, so I can't switch the whole thing over to google maps.

推荐答案

我认为这个Stackoverflow答案可以将编码的折线转换为MKPolyline。

I think this Stackoverflow answer can convert the encoded polyline to MKPolyline.

答案是一个Objective-C版本,所以我试图将它转换为Swift,示例代码:

The answer is an Objective-C version, so I tried to convert it to Swift, sample code:

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
    }

您可以尝试此GitHub链接中的示例项目

以下是使用Google方向API网络服务在旧金山到圣何塞的MKMapView上呈现路线的图像。

Below is the image of using Google Direction API web service to render a route on MKMapView from San Francisco to San Jose.

这篇关于有没有办法使用谷歌获取方向(只是路线)mkmapview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆