【问题标题】:watchPosition combined with polylineswatchPosition 结合折线
【发布时间】:2015-03-30 19:51:35
【问题描述】:

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

如果有人能发光,那将非常感激,因为我现在完全坚持这个!

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

【问题讨论】:

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


    【解决方案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 **
    }
    

    【讨论】:

    • 确实,我在疯狂尝试的过程中错过了这一点!我现在从我的 js 文件以及 VM12015 中收到错误,我只能假设与地图相关,它们是:Uncaught InvalidValueError: at index 0: not an instance of LatLngVM12015:64 Uncaught TypeError: Cannot read property 'lat' of undefined
    • 如果您有其他问题,您需要提供一个Minimal, Complete, Tested and Readable example 来证明这些问题。
    • 我想我已经解决了这个问题,用于填充坐标的数组位于 onSuccess 函数中,我还重新排列了代码并在 startTrack 之前调用了 onSuccess 并移动了周围的代码,它似乎现在正在工作。我不认为有办法增加何时检查一组新坐标?我试过使用 maximumAge,它似乎忽略了它,并在坐标改变后立即添加一个新的多边形
    猜你喜欢
    • 2014-10-10
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多