5

I was trying to develope an iOS app similar to this app.

I need to calculate the intermediate locations between two cities or two locations. I couldn't figure out which API was the best. I tried google directions API. But that donot provide locations between two locations. Can anyone suggest an API. I need to implement this in swift.

Thanks

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32

1 Answers1

4

You need to use this api for get routes between two location and cities

when you fire this api you get encoded polyline object in this dictionary

overview_polyline: {
points: "m_qkCknuyLiIgEeAw@gA_A{HgGs@i@i@|@m@jAuCdGa@z@QRy@pBYXa@t@BBf@b@tE`Aa@lJKjA?P"
},

something like above, it is encode formate of the latitude and longitude which between two points

you can get the latitude and longitude array by below methods

NSInteger index = 0, len = encoded.length;
int lat = 0, lng = 0;
while (index < len) {
    int b, shift = 0, result = 0;
    do {
        //            b = encoded.charAt(index++) - 63;
        b = [encoded characterAtIndex:index++] - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lat += dlat;
    shift = 0;
    result = 0;
    do {
        b = [encoded characterAtIndex:index++] - 63;
        result |= (b & 0x1f) << shift;
        shift += 5;
    } while (b >= 0x20);
    int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
    lng += dlng;
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:((double) lat / 1E5) longitude:((double) lng / 1E5)];
    [arrLocation addObject:loc];
}
Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • Thanks for the answer. Can you let me know how to decode the polyline in swift? – Amrit Sidhu Dec 17 '15 at 06:25
  • @AmritSidhu I am not aware about swift much but you can find the same thing in swift at stack overflow – Chirag Shah Dec 17 '15 at 06:27
  • Anyways thanks for the help. I'll try converting the code to swift – Amrit Sidhu Dec 17 '15 at 06:30
  • @AmritSidhu happy coding. And you get success then please accept the answer. So other can used the code – Chirag Shah Dec 17 '15 at 06:41
  • hey! I have converted the code into swift. I have successfully fetched all the coordinates. But now I need only the important locations between the route not every single turn in the road direction. – Amrit Sidhu Dec 17 '15 at 07:15
  • It is upto you how you used this location array it is given you all the point between tow places. – Chirag Shah Dec 17 '15 at 07:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98199/discussion-between-chirag-shah-and-amrit-sidhu). – Chirag Shah Dec 17 '15 at 09:01
  • What if I've to give few Waypoints along with origin and destination? – user5319825 Jul 13 '17 at 11:00
  • @user5319825 then it will draw path from source to destination as you provided – Chirag Shah Jul 13 '17 at 11:07
  • @AmritSidhu Can you please share then swift code here – Abin Baby Apr 24 '18 at 14:56
  • @AbinBaby you can directly convert my code into swift from this website https://objectivec2swift.com/ – Chirag Shah Apr 25 '18 at 09:40
  • @chiragshah Can I use this to solve my question listed here? https://stackoverflow.com/questions/49944054/how-can-can-i-get-equidistant-location-on-a-mkpolyline – Abin Baby Apr 25 '18 at 09:48
  • @AbinBaby no this things provide you coordinate between two place, I think you already have that. Your problem is your coordinate not fit in screen so this answer was not help you – Chirag Shah Apr 25 '18 at 09:53
  • @AmritSidhu Hi, I have a similar problem, where after decoding the polyline I have an array of coordinates, I want to get the list of cities between my source and destination. How can I achieve this? – Siddharth Sinha Sep 27 '18 at 23:52