小编nib*_*org的帖子

从Google地图提取折线到GeoJSON

我有一张Google地图。用户单击时,将放置一个“开始”标记。第二次单击将在第一次单击和第二次单击之间产生一条折线,并添加一个“结束”标记。第三次单击将另一个数据点添加到折线,并将“结束”标记移至最近的单击。没什么特别的:

map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: -34.397, lng: 150.644},
            zoom: 13
});
map.addListener('click', insertDataPoint);

polyline = new google.maps.Polyline({
            strokeColor: '#000000',
            strokeOpacity: 0.7,
            strokeWeight: 3
}); 
polyline.setMap(map);

plots = [];

... // Bunch of other code that isn't important

function insertDataPoint(e) {   
    var path = polyline.getPath();
    path.push(e.latLng);

    // Logic to set up marker or circle 
    if (plots.length == 0) {
        // Case: first click
        startMarker.setPosition(e.latLng);
        startMarker.setMap(map);
        plots.push(startMarker);                        

    } else {
        if (plots.length != 1) {
            // Case: we have …
Run Code Online (Sandbox Code Playgroud)

javascript google-maps google-maps-api-3

0
推荐指数
1
解决办法
4283
查看次数