如何使用google地图API根据交通类型计算两地之间的距离?
我需要设计类似谷歌地图本身的东西,设置起点和目的地,然后设置交通类型(汽车,步行,公共交通),然后根据它们计算距离。
我的应用程序是web应用程序,而不是移动应用程序。
发布于 2012-09-27 22:48:07
我用距离矩阵解决了这个问题,谢谢大家。
发布于 2012-09-21 16:52:11
如果你想计算两个地理坐标之间的距离,那么你可以使用超公式,.It给出两个点之间相同的距离。如果你想根据交通工具计算距离,我想你必须自己手工计算。
rad = function(x) {return x*Math.PI/180;}
distHaversine = function(p1, p2) {
var R = 6371; // earth's mean radius in km
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.toFixed(3);
}编辑:为了显示基于不同交通方式的路径,你可以像下面这样调用url。我不知道在哪里做是正确的还是不正确的。
"http://maps.google.com/maps?saddr=" + currentLatitude+","+currentLongitude+"&daddr="+fixedLatitude+","+fixedLongitude;发布于 2012-09-21 18:25:11
我想你指的是“方向”
看看这部分文档:
https://developers.google.com/maps/documentation/directions/#TravelModes
多保重,你有Usage Limits
https://stackoverflow.com/questions/12526872
复制相似问题