所以我正在尝试使用 WALKING 出行模式将我的折线捕捉到道路上,目前我有一条 WORKING 折线,当用户的地理位置移动时会更新,然后从旧位置删除旧折线并从新位置绘制一条新折线从始发地到目的地,但这是一条直线,“乌鸦飞”。
function polyLine(myPos){
var start;
if (!start){
start = geoMarker.position;
}else{
start = myPos;
}
var end = marker.position;
var pathCoords = [start, end];
var pathGo = new google.maps.Polyline({
path: pathCoords,
map: googleMap,
strokeColor: "#FF0000",
strokeOpacity: 0.7,
strokeWeight: 2
});
polyLineArray.push(pathGo);
}
我还尝试尝试通过此处的其他线程找到的各种代码片段,但无法正常工作。这就是我目前正在玩的。
function calcDist(myPos) {
var start;
if (!start){
start = geoMarker.position;
}else{
start = myPos;
}
var end = marker.position;
var request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK)
{
var steps = response.routes[0].legs[0].steps;
for(var step = 0; step < steps.length; step++)
{
polylineOptions = {
map: googleMap,
strokeColor: "#FF0000",
strokeOpacity: 0.7,
strokeWeight: 2,
path: steps[step].path
}
drawPoly = new google.maps.Polyline(polylineOptions);
}
drawPoly.getPath().push(ble);
}
});
}
所以我想知道我的语法对于步行方向是否正确,该函数成功地拉过所有坐标,因为它使用与 polyLine() 相同的变量,但在 calcDist 上根本没有画线谢谢。
编辑
做类似于谷歌地图文档的事情,仍然没有运气
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
function calcDist(myPos) {
var start;
if (!start){
start = geoMarker.position;
}else{
start = myPos;
}
var end = marker.position;
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}