2

I am trying to work out he distance between two postcodes. One post code coordinates are generic however the other postcode is stored in the database which i need to get. This code below doesn't work out the distance between the generic postcode and the one i have inputted.

var x = getDistanceFromLatLonInKm(52.482799000000000, -2.000643000000000, 52.48463500000000, -1.980759000000000);

function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2){
  console.log(lat1, lon1, lat2, lon2); 
var distanceFromSpecificPoint = getDistanceFromLatLonInKm.bind(null, 52.486637, -1.890952); 

  var R = 6371; // Radius of the earth in km 
  var dLat = deg2rad(lat2 - lat1); // deg2rad below 
  var dLon = deg2rad(lon2 - lon1);
  var a =
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  var d = R * c; // Distance in km 
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI / 180)
}


document.getElementById('result').textContent = x;

console.log(x)
</script>
  • http://www.zipcodeapi.com/ – Richard Hamilton Apr 05 '16 at 14:36
  • What is this line attempting to do? Seems odd to me. Wrong function signature and it doesn't seem to be adding any value: `var distanceFromSpecificPoint = getDistanceFromLatLonInKm.bind(null, 52.486637, -1.890952);` – ManoDestra Apr 05 '16 at 15:29
  • Also, you may wish to place some brackets in your calculation for `a`. Haven't checked the mathematical validity of these calculations so far, but that's one possible area for error that immediately comes to mind. And you could use some calls to Math.pow in that calculation also to avoid doing the two sin operations twice. – ManoDestra Apr 05 '16 at 16:53
  • Based on the following algorithm (I assume this is where you derived your code from?), your code looks correct: http://www.movable-type.co.uk/scripts/latlong.html. Only difference is that he's returning the value in meters and yours returns kilometers. Assuming that the algorithm is correct, then your code seems right. What's your error? Your javascript function is correct. If you wish to get a distance, simply call your javascript function passing in the general lat/lon with the lat/lon that you get from the database. Your call above works fine for me: 1.3618590385878444 km. – ManoDestra Apr 05 '16 at 17:19
  • @ManoDestra I understand it works like that, however i am trying to get the code to have one generic postcode (one postcode always set) and the other postcode to change according to which property it is and then work the distance from each property – Amandeep Singh Apr 05 '16 at 17:27
  • Then, either pass the fixed lat/lon to the javascript function, or set it as a const in the javascript function dynamically from the database via your JSP, ASP, or whatever. You can then just change the function signature to only accept one pair of lat/lon only, as the values for the other will be stored inside the function itself. Make sense? I can show you something along the lines of what I mean as an answer, if you wish? – ManoDestra Apr 05 '16 at 17:32
  • @ManoDestra yes please could you show me – Amandeep Singh Apr 05 '16 at 17:34

1 Answers1

1

As per above, you can simply get the value from the database on the server side and use whatever server side web framework you wish. The example I've given here is Classic ASP, purely as an example to show you where the values are entered. Then on the server side, the database values are constants and you only enter the lat/lon for one point and get the distance from the database lat/lon (the constants LAT1/LON1 here). dbModel would be some object on the server side which is populated from the database. You can then grab the latitude/longitude values from this object for insertion into the web page via server side scripting.

function getDistanceFromLatLonInKm(lat, lon) {
    const EARTH_RADIUS = 6371; // Radius of the earth in km 
    // Here's where you would add the value dynamically from the DB.
    // I'm using classic ASP here just as an example. You'll have to
    // amend for your particular server side web framework, be it
    // JSP, MVC, etc.
    const LAT1 = <%=dbModel.getLatitude()%>;
    const LON1 = <%=dbModel.getLongitude()%>;
    console.log(LAT1, LON1, lat, lon);

    var dLat = deg2rad(lat - LAT1);
    var dLon = deg2rad(lon - LON1);
    var a = Math.pow(Math.sin(dLat / 2), 2) + Math.cos(deg2rad(LAT1)) * Math.cos(deg2rad(LON1)) * Math.pow(Math.sin(dLon / 2), 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = EARTH_RADIUS * c; // Distance in km 

    return d;
}

function deg2rad(deg) {
    return deg * (Math.PI / 180);
}

Or, if you don't require it to be dynamic like this, just grab the static values from the database and enter them here as the values for LAT1 and LON1.

E.g.

    const LAT1 = 52.482799;
    const LON1 = -2.000643;

Then just call your function like this...

var distance = getDistanceFromLatLonInKm(52.48463500000000, -1.980759000000000);

To do PAF lookups:

FreeMapTools

Google Maps. Just search your postcode, then grab the latitude/longitude from the URL.

ManoDestra
  • 6,325
  • 6
  • 26
  • 50
  • Works fine for me. What's the error you're getting or why is it not working? You'll need to tell me what's going wrong so that I can help you. – ManoDestra Apr 05 '16 at 18:07
  • There was a minor typo when I transferred the code. Updated now :) – ManoDestra Apr 05 '16 at 18:14
  • Thanks. i am using php to get the information from the database. Im not sure how i will get the postcode information as the property postcode is stored without the long lat information. However i have all the Uk postcodes with there respective long and lat information in another table called postcodes. So how would i get the property postcode from properties table then link it to the postcodes table then get that information into this formula ? – Amandeep Singh Apr 05 '16 at 18:14
  • You'll need to do a Postcode lookup, if you don't have the lat/lon in your database, prior to entering the lat/lon values here via PHP. http://stackoverflow.com/questions/2832942/uk-royal-mail-paf-address-finder-via-postcode-alternatives. Once you have the lat/lon values, just enter them here and it will work fine. It would appear that your issue is not about javascript at all, but really about how to do a postcode lookup and convert to lat/lon. – ManoDestra Apr 05 '16 at 18:16
  • Thanks for the example, it seems to be working now. Yeah you have suspected right, that is the real issue and I'm not exactly sure on how to go about that? if you don't mind could you give me an example or starting point for that too? – Amandeep Singh Apr 05 '16 at 18:23
  • I posted a link for you above. Been a long time since I've done any PAF lookups TBH, so I don't know how they're done now, costs, etc. You'll have to look it up. Or you could just use [THIS](https://www.freemaptools.com/convert-uk-postcode-to-lat-lng.htm). Or [Google Maps](http://www.google.com/maps) for that matter. Just enter you postcode, go to the location, then grab the lat/lon out of the URL. – ManoDestra Apr 05 '16 at 18:26