Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

calculate distance between two postcodes inside a service success mapping

Is it possible to calculate the distance between two postcodes inside a success mapping?

I have a collapsible component in in a query mobile app which I want to display the distance between two postcodes in the header of the collapsible.

One postcode will be fixed and the other will change in each iteration of the loading service, so it needs to be calculated for each iteration.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

calculate distance between two postcodes inside a service success mapping

Hi Terry,

Include the GoogleMaps API in your project (let me know if you need to know how to do that).

GeoCode the addresses (just send the postal code) using a call to the API described here:

https://developers.google.com/maps/do...

Once you have two geoPoints you can use one of these function to compute distance:(one uses the coordinates the other your coordinate objects)... look me up further if you need more help.... and yes you can do this in your success mapping - to make it easier to debug - in your success mapping -- do this:

fDoMyComputations( data ) ; // send the data from the successful call to your function

in a separate Javascript asset - create these functions

function fDoMyComputations( oInBoundData) {
// Read the postal codes, create google objects for the postal codes
// and then compute the distance using the function or functions below :-) and of course update your data in your tab at the same time.... :-)
}

function fDistance(lat1, lon1, lat2, lon2, unit) {
var dist = 0;
try {
var radlat1 = Math.PI * lat1 / 180;
var radlat2 = Math.PI * lat2 / 180;
var theta = lon1 - lon2;
var radtheta = Math.PI * theta / 180;
dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180 / Math.PI;
dist = dist * 60 * 1.1515;
if (unit == "K") {
dist = dist * 1.609344;
}
if (unit == "N") {
dist = dist * 0.8684;
}
dist = Math.abs(dist);
} catch (e) {
console.warn('[JS-WARN] Error in fDistance : ', e.message);
}
return dist;
}

function fDistanceCoords(oInBoundLocation1, oInBoundLocation2, unit) {
try {
return fDistance(oInBoundLocation1.coords.latitude, oInBoundLocation1.coords.longitude, oInBoundLocation2.coords.latitude, oInBoundLocation2.coords.longitude, unit);
} catch (e) {
console.warn('[JS-WARN] Error in fDistanceCoords : ', e.message);
return 0;
}
}

Hope that helps.... I'm sure there are lots of ways to do this --- but I do know this works on UK and US postal codes....

If you need someone to code it for you - my son works for me at a low hourly rate - we can get er done pretty darned quickly...and we have no minimums ...

Best,

bruce

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

calculate distance between two postcodes inside a service success mapping

Thanks Bruce, i will have a go at that

Return to “Issues”