3 // The first support methods that don't (yet) exist in v3
4 google.maps.LatLng.prototype.distanceFrom = function (newLatLng) {
6 var EarthRadiusMeters = 6378137.0; // meters
9 var lat2 = newLatLng.lat();
10 var lon2 = newLatLng.lng();
11 var dLat = (lat2 - lat1) * Math.PI / 180;
12 var dLon = (lon2 - lon1) * Math.PI / 180;
13 var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
14 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
15 var d = EarthRadiusMeters * c;
19 google.maps.LatLng.prototype.latRadians = function () {
21 return this.lat() * Math.PI / 180;
24 google.maps.LatLng.prototype.lngRadians = function () {
26 return this.lng() * Math.PI / 180;
30 * Returns the length of a path in metres.
33 google.maps.Polygon.prototype.Distance = function () {
36 for (i = 1; i < this.getPath().getLength(); i++) {
37 dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i - 1));
43 * Returns GLatLng of the point at the given distance along the path. Returns null if the path is shorter than the specified distance.
44 * @method GetPointAtDistance
45 * @param metres {Integer} distance in metres
47 google.maps.Polygon.prototype.GetPointAtDistance = function (metres) {
49 // some awkward special cases
51 return this.getPath().getAt(0);
56 if (this.getPath().getLength() < 2) {
60 var dist = 0, olddist = 0, i = 0;
62 for (i = 1; (i < this.getPath().getLength() && dist < metres); i++) {
64 dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i - 1));
69 var p1 = this.getPath().getAt(i - 2);
70 var p2 = this.getPath().getAt(i - 1);
71 var m = (metres - olddist) / (dist - olddist);
72 return new google.maps.LatLng(p1.lat() + (p2.lat() - p1.lat()) * m, p1.lng() + (p2.lng() - p1.lng()) * m);
76 * Returns the Vertex number at the given distance along the path. Returns null if the path is shorter than the specified distance.
77 * @method GetIndexAtDistance
78 * @param metres {Integer} distance in metres
80 google.maps.Polygon.prototype.GetIndexAtDistance = function (metres) {
82 // some awkward special cases
84 return this.getPath().getAt(0);
90 for (i = 1; (i < this.getPath().getLength() && dist < metres); i++) {
91 dist += this.getPath().getAt(i).distanceFrom(this.getPath().getAt(i - 1));
99 // Copy all the above functions to GPolyline
100 google.maps.Polyline.prototype.Distance = google.maps.Polygon.prototype.Distance;
101 google.maps.Polyline.prototype.GetPointAtDistance = google.maps.Polygon.prototype.GetPointAtDistance;
102 google.maps.Polyline.prototype.GetIndexAtDistance = google.maps.Polygon.prototype.GetIndexAtDistance;