谷歌地图 – 如何获得米之间的两点之间的距离?
我有这些坐标:
(45.463688, 9.18814) (46.0438317, 9.75936230000002)
我需要(使用Google API V3,我认为)以米计算这两点之间的距离。 我该怎么做?
如果您正在使用v3 google maps API,请使用以下函数:注意:您必须将&libraries=geometry
添加到您的脚本源
<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>
我认为你可以没有任何特定的API,并用简单的Javascript来计算距离:
这个网站有关于地理计算和Javascript样本距离计算的好信息。
好的,快速浏览一下Google API页面,看起来,你可以这样做:
- 调用DirectionsService()。route()以获取路线的DirectionsResult
- 对于一条或每条路线通过其腿属性并计算距离的总和
这主要是用google api以外的math来完成的,所以除了find正确的坐标外,你不需要使用API。
知道,这里有一个链接到以前回答与Javascript有关的问题。
计算2个GPS坐标之间的距离
尝试这个
CLLocationCoordinate2D coord1; coord1.latitude = 45.463688; coord1.longitude = 9.18814; CLLocation *loc1 = [[[CLLocation alloc] initWithLatitude:coord1.latitude longitude:coord1.longitude] autorelease]; CLLocationCoordinate2D coord2; coord2.latitude = 46.0438317; coord2.longitude = 9.75936230000002; CLLocation *loc2 = [[[CLLocation alloc] initWithLatitude:46.0438317 longitude:9.75936230000002] autorelease]; CLLocationDistance d1 = [loc1 distanceFromLocation:loc2];
尝试这个:
const const toRadians = (val) => { return val * Math.PI / 180; } const toDegrees = (val) => { return val * 180 / Math.PI; } // Calculate a point winthin a circle // circle ={center:LatLong, radius: number} // in metres const pointInsideCircle = (point, circle) => { let center = circle.center; let distance = distanceBetween(point, center); //alert(distance); return distance < circle.radius; }; const distanceBetween = (point1, point2) => { //debugger; var R = 6371e3; // metres var φ1 = toRadians(point1.latitude); var φ2 = toRadians(point2.latitude); var Δφ = toRadians(point2.latitude - point1.latitude); var Δλ = toRadians(point2.longitude - point1.longitude); var a = Math.sin(Δφ / 2) * Math.sin(Δφ / 2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; }
参考文献: http : //www.movable-type.co.uk/scripts/latlong.html