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

前端 未结 3 1055
既然无缘
既然无缘 2021-01-15 06:53

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 loca

3条回答
  •  难免孤独
    2021-01-15 07:10

    Swift 3 version, slightly condensed, thanks to ztan.

    If you want to use Googles geo polylines in iOS without 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
    }
    

提交回复
热议问题