How to Calculate the Distance Between Two Gps Coordinates Without Using Google Maps API

How to calculate the distance between two GPS coordinates without using Google Maps API?

Distance between two coordinates on earth is usually calculated using Haversine formula. This formula takes into consideration earth shape and radius. This is the code I use to calculate distance in meters.

def distance(loc1, loc2)
rad_per_deg = Math::PI/180 # PI / 180
rkm = 6371 # Earth radius in kilometers
rm = rkm * 1000 # Radius in meters

dlat_rad = (loc2[0]-loc1[0]) * rad_per_deg # Delta, converted to rad
dlon_rad = (loc2[1]-loc1[1]) * rad_per_deg

lat1_rad, lon1_rad = loc1.map {|i| i * rad_per_deg }
lat2_rad, lon2_rad = loc2.map {|i| i * rad_per_deg }

a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))

rm * c # Delta in meters
end

puts distance([46.3625, 15.114444],[46.055556, 14.508333])
# => 57794.35510874037

Calculate distance between two points in google maps V3

If you want to calculate it yourself, then you can use the Haversine formula:

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
};

How do i find distance between one place to another using Geolocation or Similiar API without embedding a Google Map?

If you're not going to display a map, then you can't use Google Maps API (it violates their TOS).

If you are looking to get the lat/lon from an address without Google Maps or similar (because similar services have similar TOS) then you'll want to look for something like LiveAddress API (and apparently I'm supposed to disclose that I work at SmartyStreets)

This example works for US addresses. International addresses require a different API.

An API like the US Street Address API doesn't require you to show a map, returns geo coordinates, and will verify the validity of the address as it returns its payload.

Here's a Javascript example.

    <script type="text/javascript" src="liveaddress.min.js"></script>
<script type="text/javascript">
LiveAddress.init(123456789); // API key

// Make sure you declare or obtain the starting or ending lat/lon somewhere.
// This example only does one of the points.

LiveAddress.geocode(addr, function(geo) {
var lat2 = geo.lat, lon2 = geo.lon;

// Distance calculation from: http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
var R = 6371; // Radius of the earth in km
var dLat = (lat2-lat1).toRad(); // Javascript functions in radians
var dLon = (lon2-lon1).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) *
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
});
</script>

How to calculate distance between two locations using their longitude and latitude value

Here getting distance in miles (mi)

private double distance(double lat1, double lon1, double lat2, double lon2) {
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));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}

private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}

Calculate shortest distance between a GPS coordinate and a route

One appoach I can think of is to translate the route into a set of GPS
coordinates and calculate the distance to each coordinate. Is there a
better way of doing this?

Yes there is a much better way.

The first part is correct: translate to a polyline of lat/lon coordinates.
However the second is to simple. You want the shortest distance to the line segment not the the next corner point, that is the start or end of the line.

In school you used the hessian normal distance. However this formula calculates just the distance to an infinite line. In case of a polyline you have an sequence of line segments.

So you need a formula for "distance to line segment". This one you can find using that search terms.

Having this formula implemented, you interate over all line segments each defined by two points (polyPoint[i], polyPoint[i+1]) and take the minimum of the distance.

Don't forget to transform the current line segement to cartesian (x,y) space, because the usualy formulas, do not work on spehrical coordinates.

This approach is more complex, but gives exact results. Remember a line on a route if often 400-700 meters long. So the simple approach you asked, gives in this case an error of 200-350m if you are in the middle between the two points.

Google Maps - How to get the distance between two point in metre?

If you're looking to use the v3 google maps API, here is a function to use:
Note: you must add &libraries=geometry to your script source

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>

<script>
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);

alert(calcDistance(p1, p2));

//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}

</script>

How to verify the distance between 2 coordinates?

You can also use the Google Maps API Geometry Library.

firstLatLng = new google.maps.LatLng(37,-122);
secondLatLng = new google.maps.LatLng(37.386337, -122.085823);
distance = google.maps.geometry.spherical.computeDistanceBetween(firstLatLng,secondLatLng);


Related Topics



Leave a reply



Submit