There is a method in GMap3. It's a static method of the google.maps.geometry.spherical namespace.
INPUT: two Latitude+Longitude [LatLong] objects.
The function will utilize a default Earth radius of 6378137 meters. If necessary,
the default radius can be overridden with a custom value .
In the HTML <Head>, be sure to include:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=falsev=3&libraries=geometry" />
The call will look like this:
google.maps.geometry.spherical.computeDistanceBetween (latLongA, latLongB);
=========================================================
NOTE: for more info on the Google GMap3, see http://gmap3.net/quickstart.html
=========================================================
Example using GPS latitude/longitude of 2 points. (Distance results are expressed in meters.)
var latitude1 = 39.06;
var longitude1 = -0.26;
var latitude2 = 40.41;
var longitude2 = -3.67;
var distance = google.maps.geometry.spherical.computeDistanceBetween(new google.maps.LatLng(latitude1, longitude1), new google.maps.LatLng(latitude2, longitude2));
========================================================
Alternatively... Add this to the beginning of your JavaScript code:
google.maps.LatLng.prototype.distanceFrom = function(latlng) {
var lat = [this.lat(), latlng.lat()]
var long = [this.lng(), latlng.lng()]
var Rad = 6378137;
var dLat = (lat[1]-lat[0]) * Math.PI / 180;
var dLong = (long[1]-long[0]) * Math.PI / 180;
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat[0] * Math.PI / 180 ) * Math.cos(lat[1] * Math.PI / 180 ) *
Math.sin(dLong/2) * Math.sin(dLong/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = Rad * c;
return Math.round(d);
}
...then use the 'distanceFrom()' function like this:
var loc1 = new GLatLng(52.5773139, 1.3712427);
var loc2 = new GLatLng(52.4788314, 1.7577444);
var dist = loc2.distanceFrom(loc1);
alert(dist/1000);
===============================================
Here is the c# implementation of the formula:
public class DistanceAlgorithm
{
const double PIx = 3.141592653589793;
const double RADIO = 6378.16;
/// <summary>
/// This class is static -it cannot be instantiated.
/// </summary>
private DistanceAlgorithm() { }
/// <summary>
/// Convert degrees to Radians
/// </summary>
/// <param name="x">Degrees</param>
/// <returns>The equivalent in radians</returns>
public static double Radians(double x)
{
return x * PIx / 180;
}
/// <summary>
/// Calculate the distance between two places.
/// </summary>
/// <param name="lon1"></param>
/// <param name="lat1"></param>
/// <param name="lon2"></param>
/// <param name="lat2"></param>
/// <returns></returns>
public static double DistanceBetweenPlaces(
double lon1,
double lat1,
double lon2,
double lat2)
{
double dlon = Radians(lon2 - lon1);
double dlat = Radians(lat2 - lat1);
double a = (Math.Sin(dlat / 2) * Math.Sin(dlat / 2)) + Math.Cos(Radians(lat1)) * Math.Cos(Radians(lat2)) * (Math.Sin(dlon / 2) * Math.Sin(dlon / 2));
double angle = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
return (angle * RADIO) * 0.62137;//distance in miles
}
}
=======================================================================
// NOTE: p1 and p2 are google.maps.LatLng(x,y) objects
function calcDistance(p1, p2) {
var d = (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
console.writeline(d);
var name = console.ReadLine();
}
=====================================================
// Using PHP, you can calculate the distance using this simple 'calculate_distance' function
// to calculate distance between two lat & long
function calculate_distance($lat1, $lon1, $lat2, $lon2, $unit='N')
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
// function ends here
==================================================
No comments:
Post a Comment