watchPosition与多段线组合

watchPosition combined with polylines

本文关键字:组合 段线 watchPosition      更新时间:2023-09-26

我正试图找到一种在坐标变化时添加多段线的方法,我已经尝试了我能想到的所有可能的猜测组合,还包括谷歌自己的"复杂多段线"文档,但我运气不佳。

如果有人能发光,那就太好了,因为我现在完全陷入了困境!

function startTrack() {
    var options = { enableHighAccuracy: true, maximumAge: 0, timeout : 5000 };
    watchID = navigator.geolocation.watchPosition(onSuccessTrack, onErrorTrack, options);
    var polyOptions = {
        strokeColor: '#000000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    };
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
}
function onSuccessTrack(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
var path = poly.getPath();
path.push(google.maps.LatLng(latitude, longitude));
}

您必须更新多段线的路径:

function startTrack() {
    var options = { enableHighAccuracy: true, maximumAge: 0, timeout : 5000 };
    watchID = navigator.geolocation.watchPosition(onSuccessTrack, onErrorTrack, options);
    var polyOptions = {
        strokeColor: '#000000',
        strokeOpacity: 1.0,
        strokeWeight: 3
    };
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
}
function onSuccessTrack(position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
  var path = poly.getPath();
  path.push(google.maps.LatLng(latitude, longitude));
  poly.setPath(path); // ** update path for polyline **
}