Thursday 24 May 2018

calculate distance between two points on map using JavaScript

calculate the distance between two locations using GeoDataSource
Description:
In this example we explain that how to calculate distance between two points on Bing Map or Google Map using JavaScript. Or how to measure distance between points on Map using JavaScript.or JavaScript to calculate distance between two points on Map. Or how to calculate kilometers or miles between two points on Maps using JavaScript. Or how to calculate kilometer or miles between two pin point or two pushpins on maps using JavaScript.

So here we demonstrate that how to calculate distance or measure between two routes points using JavaScript. Or routines calculate the distance between two points (using Latitude and Longitude of those points) using JavaScript.so how to calculate distance between two location or two cities or two addresses using JavaScript.



Here you have to pass Latitude and Longitude of source and destination points or city (if you want to get the Latitude and Longitude of the Address we have already explain that in previous example follow this link Get Latitude and Longitude From Address) and last parameter unit is used to get output as in kilometer or miles as per your requirement if you pass “K” then it will return output in kilometer if you pass “N” then it will return output in Miles.

Function: 

function distance(lat1, lon1, lat2, lon2, unit) {
            var radlat1 = Math.PI * lat1 / 180
            var radlat2 = Math.PI * lat2 / 180
            var theta = lon1 - lon2
            var radtheta = Math.PI * theta / 180
            var 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 }
            return dist
        }



2 comments: