Amrit Sidhu Amrit Sidhu - 5 months ago 26
iOS Question

Get way Points between two locations

I was trying to develope an iOS app similar to https://itunes.apple.com/fr/app/blablacar-trusted-ridesharing/id341329033?l=en&mt=8

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

Answer

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