Reputation: 8952
I am looking to add a feature to a order entry system to show the distance in KM between the order location and assigned delivery van in real time.
The vans have a GPS Tracking system from High Point GPS, comes with an API to query driver location, returns in format of LAT/LONG, and location address if available.
Once I get the location I can use a web service to calculate the distance between the 2 locations, does anyone know what is the best way to get the distance between 2 locations using LAT/LONG for vehicle and address lookup for the delivery location?
Upvotes: 3
Views: 3428
Reputation: 1519
I've done this but in Java, probably you can port it to c# with no much effort.
This is very accurate for distances shorter than 2000 kilometers, then it can vary a little bit from the real distance. This is due to the earth curvature. But for small distances you can assume it is plain with none or very small impact in the result.
Here is a link I found useful.
And another link, an implementation of Haversine in C#.
Hope it helps.
Best regards, Federico.
public String execute(String plat1, String plon1, String plat2, String plon2) {
String distance;
double lat1, lon1, lat2, lon2;
try{
lat1 = Double.parseDouble(plat1);
lon1 = Double.parseDouble(plon1);
lat2 = Double.parseDouble(plat2);
lon2 = Double.parseDouble(plon2);
} catch (Exception e){
lat1 = 0.0d;
lon1 = 0.0d;
lat2 = 0.0d;
lon2 = 0.0d;
}
//theta and distance
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
//distance
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
dist = dist * 1.609344;
//convert to meters
dist = dist * 1000;
//output in meters
distance = Double.toString(dist);
return distance;
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
A little explanation:
Upvotes: 1
Reputation: 15836
The simple answer you may find , is a simple formula :
Calculate distance between two points in google maps V3
var rad = function(x) {
return x * Math.PI / 180;
};
var getDistance = function(p1, p2) {
var R = 6378137; // Earth’s mean radius in meter
var dLat = rad(p2.lat() - p1.lat());
var dLong = rad(p2.lng() - p1.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(rad(p1.lat())) * Math.cos(rad(p2.lat())) *
Math.sin(dLong / 2) * Math.sin(dLong / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d; // returns the distance in meter
};
using google maps api will be the best solution:
https://developers.google.com/maps/documentation/distancematrix/
Upvotes: 1
Reputation: 4352
Create two DbGeography instance for source and destination point (http://msdn.microsoft.com/en-us/library/system.data.spatial.dbgeography.distance(v=vs.110).aspx). Then use distance method of DbGeography for find the distance between two points.
For distance unit refer this post : System.Data.Spatial DbGeography.Distance units?
Upvotes: 2
Reputation: 271
You can use Google Maps to get Lat/Long of address See answer by Thomas Clayson
How can I get latitude, longitude of a location programmatically or using a api
Then you can calculate the distance between the two sets of coordinates by using the Law of Cosines or the Haversine formula See Law of Cosines
Upvotes: 7
Reputation: 7829
It depends on the other services you are already using. For example, we already use google maps to display the route, so we use their geocoding service to translate from an address to coordinates, as well as their distance service to compute distances.
Upvotes: 5