当对同一来源和目的地进行不同调用时,Google Distance API会给我不同的结果
Google Distance API giving me different results when called differently for the same source and destinations
我正试图从googleapi中用两种不同的方法来找到两点p1和p2之间的距离。。。
这是代码。。。
var directionsService = new google.maps.DirectionsService();
var p1 = new google.maps.LatLng(45.463688, 9.18814);
var p2 = new google.maps.LatLng(46.0438317, 9.75936230000002);
var request = {
origin: p1,//'Melbourne VIC', // a city, full address, landmark etc
destination: p2,// 'Sydney NSW',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
console.log(response.routes[0].legs[0].distance.value/1000); // the distance in metres
//console.log(response.routes[0].legs[0].duration.value);
}
else {
// oops, there's no route between these two locations
// every time this happens, a kitten dies
// so please, ensure your address is formatted properly
}
console.log(calcDistance(p1, p2));
//if(!(parseFloat("hello"))) console.log(parseFloat("12.222"));
//calculates distance between two points in km's
function calcDistance(p1, p2) {
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 1000).toFixed(2);
}
});
我得到的输出如下。。。103.847//对于第一个日志78.35//对于第二个日志
有人能解释一下为什么会有这种差异吗。。。。我是不是搞错了。。。。
您使用的第一种方法是请求谷歌方向服务,当您将行驶模式设置为DRIVING
时,该响应是道路上两点之间的一组方向。距离就是你必须行驶的距离。第二种方法是返回几何体库,返回球体上的最短路径,即两点之间的世界(大圆距离)。因此,距离2更短,因为它是直接的,而距离1是假设你想使用道路的实际行驶距离。
相关文章:
- 如何更改bigquery API中的计费层选项
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 使用Facebook live API创建实时视频对象时的隐私设置
- 谷歌放置API:按国家或餐馆名称搜索餐馆
- MeteorJS:在带有回调的vzaar api上正确使用wrapAsync
- 是否可以控制获取哪些Google地图脚本(JavaScript API)
- API密钥使用和检查示例
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- 谷歌地图JS API+JSON-多个标记没有显示
- 单击超链接时,如何使用Google Maps API v3缩放地图
- 如何在 API 调用后和 if 语句中启用提交按钮
- 与运行长作业(javascript,node.js)的第三方API同步的最佳实践
- 搜索api在mac上显示对话框
- Backbone.js restful json API design
- 标记的实时更新,无需加载页面谷歌地图API V3
- 当对同一来源和目的地进行不同调用时,Google Distance API会给我不同的结果
- 谷歌地图Api附近搜索-为什么Google.Maps.places.RankBy.DISTANCE返回的结果更少
- 为什么不是'当使用Google Distance Matrix Service API时,不会调用我的回调函数