Javascript 获取当前位置和计算路线之间的距离
我有个问题。 我想计算当前位置和计算路线之间的距离。我正在使用谷歌地图API v3计算路线。我曾尝试使用google.maps.geometry.poly.isLocationOnEdge,但没有成功 这是我的密码:Javascript 获取当前位置和计算路线之间的距离,javascript,google-maps,Javascript,Google Maps,我有个问题。 我想计算当前位置和计算路线之间的距离。我正在使用谷歌地图API v3计算路线。我曾尝试使用google.maps.geometry.poly.isLocationOnEdge,但没有成功 这是我的密码: directionsService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK){ var poly = new goog
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK){
var poly = new google.maps.Polyline({
paths:new Array(result.routes[0].overview_path)
});
var currentPosition = new google.maps.LatLng(51.574470000000005, 4.32713);
if(google.maps.geometry.poly.isLocationOnEdge(currentPosition, poly)){
console.log("On route");
}else{
console.log("Not on route");
}
var directionsDisplay = new google.maps.DirectionsRenderer({
polylineOptions: {
strokeColor: color,
strokeWeight: 3
}
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(result);
}
});
我不确定这是不是得到我想要的结果的好方法
有人能帮我吗?首先,您可能需要在
isLocationOnEdge
方法中设置公差。也许这一点有点离题了。默认公差为10^-9,这非常小。有关更多详细信息,请参阅
你真的需要到路线的距离吗?或者知道标记是否在你的路线上就足够了
如果是这样,你可能想看看这个
建议使用containsLocation()
方法
如果你真的想计算距离,那么我不得不说这是不可能的,根据谢谢!我要试试看。让你随时了解最新情况!它起作用了!问题在于概览路径。//如果(!google.maps.geometry.poly.isLocationOnEdge(新的google.maps.LatLng(接收到的_数据['latitude'],接收到的_数据['latitude']),新的google.maps.Polyline({path:result.routes[0].overview_path}),.00001*reach]){},请检查位置是否在路线上