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


i'm using swift on ios , using mkmapview. i've been working on giving user - textfield , letting user have form of route between , locations. i've got working on mkmapview using built in directions , geocoding api calls apple. after using while realized lot of countries not supported apple apple - supported countries directions

so there way routes google maps sdk , translate them mkpolyline or mkoverlay? haven't worked google maps keep in mind. saw question iphone: using google directions has answer, newest solution 4 years old , in objective-c. although understand how convert objective-c code swift, i'd rather not have go through whole project. keep in mind part of bigger project, can't switch whole thing on google maps.

i think this stackoverflow answer can convert encoded polyline mkpolyline.

the answer objective-c version, tried convert swift, sample code:

func polylinewithencodedstring(encodedstring: string) -> mkpolyline {         let bytes = (encodedstring 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              {                 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              {                 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)                 index in 0..<count {                     coords[index] = temp[index]                 }                 temp.destroy()                 count = newcount             }          }          let polyline = mkpolyline(coordinates: coords, count: coordidx)         coords.destroy()          return polyline     } 

you can try sample project this github link.

below image of using google direction api web service render route on mkmapview san francisco san jose.

enter image description here


Comments