Thanks everyone!

Chad, I tested your code, marker1LatLng.distanceFrom (marker1LatLng)
does not return 0.

I finally come up with the following code, I tested it return 0 for
the same LatLng.
The result is return in "meters", which is same as Google Map v2, but
the result is not except the same with v2, but similar I think.

Thanks,
Leonardo Wong

google.maps.LatLng.prototype.distanceFrom = function(newLatLng) {
  //var R = 6371; // km (change this constant to get miles)
  var R = 6371000; // meters
  var lat1 = this.lat();
  var lon1 = this.lng();
  var lat2 = newLatLng.lat();
  var lon2 = newLatLng.lng();
  var dLat = (lat2-lat1) * Math.PI / 180;
  var dLon = (lon2-lon1) * Math.PI / 180;
  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);
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var d = R * c;
  return d;
}





Reference:
http://en.wikipedia.org/wiki/Earth_radius
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/f84629b7c774082b/5afb9bb86cbf966e?lnk=gst&q=distance#5afb9bb86cbf966e
http://www.barattalo.it/2009/12/19/ruler-for-google-maps-v3-to-measure-distance-on-map/

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps JavaScript API v3" group.
To post to this group, send email to google-maps-js-api...@googlegroups.com.
To unsubscribe from this group, send email to 
google-maps-js-api-v3+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-maps-js-api-v3?hl=en.

Reply via email to