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