# Distance between 2 sets of coordinates Haversine Function

## Recommended Posts

Does anyone have a Haversine Function that works?  I'm having trouble, and this is where i'm at:

```<script type="text/javascript">

function f_Haversine_Roving(){

var lat1 = document.getElementById("InsertRecordLat").value;
var lon1 = document.getElementById("InsertRecordLng").value;
var lat2 = document.getElementsByName("cbParamVirtual6”)[0].value;
var lon2 = document.getElementsByName("cbParamVirtual7”)[0].value;

var R = 6371; // km
var dLat = (lat2-lat1)*Math.PI/180;
var dLon = (lon2-lon1)*Math.PI/180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.asin(Math.sqrt(a));
var d = R * c;
d= (d*1000); //in meters

document.getElementById("InsertRecordDistance").value=d;

document.getElementById("caspioform").onsubmit=f_Haversine_Roving();}

});

</SCRIPT>```

##### Share on other sites

Still struggling with this, here is where I am now:

```<script type="text/javascript">

var lat1 = document.getElementById("InsertRecordLat").value;
var lon1 = document.getElementById("InsertRecordLng").value;
var lat2 = document.getElementsByName("cbParamVirtual6”)[0].value;
var lon2 = document.getElementsByName("cbParamVirtual7”)[0].value;

var R = 6371; // km
var dLat = (lat2-lat1)*Math.PI/180;
var dLon = (lon2-lon1)*Math.PI/180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1*Math.PI/180) * Math.cos(lat2*Math.PI/180) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.asin(Math.sqrt(a));
var d = R * c;
d= (d*1000); //in meters

document.getElementById("InsertRecordDistance”).value=d;

});

</SCRIPT>```

##### Share on other sites

@Hastur @Vitalikssssssany ideas?

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.