3

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?

Israel Margulies
  • 8,656
  • 2
  • 30
  • 26
  • 2
    Hi Israel, did you call the customer care of HighPoint GPS? What did they say? – George Philip Jan 27 '14 at 17:55
  • Yes, they do support the API to get the LAT/LONG of vehicle, but I need to get the distance to the order location for my application – Israel Margulies Jan 29 '14 at 16:33
  • Are you looking for the straight line distance or the driving distance? Since we are talking about vehicles and destinations, it would seem more like the latter, which is a very different problem to solve than the former. The van might be 100 yards away but be on the wrong side of some railroad tracks and have to drive a couple miles to get to the destination. – cdkMoose Apr 11 '14 at 16:53
  • Both would be great to have. – Israel Margulies Apr 13 '14 at 03:40
  • 1
    Bing Maps offers an API to query distance in KM or Miles [Here is a similar question with resolutions][1] [1]: http://stackoverflow.com/questions/9856101/calculating-the-driving-distance-between-two-points-using-php-only-bing-maps – user2141606 Dec 18 '14 at 04:26

5 Answers5

7

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

Community
  • 1
  • 1
GreatJobBob
  • 271
  • 1
  • 8
5

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.

Lorenzo Dematté
  • 7,638
  • 3
  • 37
  • 77
2

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?

Community
  • 1
  • 1
petchirajan
  • 4,152
  • 1
  • 18
  • 20
1

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:

  • (plat1, plon1) is lat and lng of point 1 or origin
  • (plat2, plon2) is lat and lng of point 2 or destination
  • the method "execute" is the one you call, it returns a string containing the distance in meters (conversion to other units can be done easily)
  • two assisting functions are declared as "deg2rad" and "rad2deg".
Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30
1

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/

Community
  • 1
  • 1
ProllyGeek
  • 15,517
  • 9
  • 53
  • 72