watchPosition结合折线

时间:2015-03-30 19:51:35

标签: javascript google-maps google-maps-api-3 geolocation

我正在尝试找到一种方法来添加折线,因为坐标会发生变化,我已经尝试了所有可能的猜测组合,我可以想到,还包括谷歌自己的文档"复杂折线"我没有运气。

如果有人能发光,那么它会非常受欢迎,因为我现在已经完全陷入困境了!

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));
}

1 个答案:

答案 0 :(得分:1)

您必须更新折线的路径:

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 **
}