2017-10-17 150 views
2

我想在已加載的點和右擊標記點之間添加行。我已經提到了Mapbox的例子,並且已經到了這個階段。我只有一條線,我第一次執行操作。我需要每個操作的行。是操作的順序如下:GeoJson LineString在Mapbox地圖中第一次點擊後只加載一次

  1. 上加載點(點從GeoJSON的加載)左鍵點擊
  2. 右鍵點擊地圖上的任意位置。 這應該在右鍵單擊點創建一個標記,並將其與之前離開點的點相連接。

我希望得到一些幫助。這是我在SO的第一篇文章。請原諒我的錯誤。先謝謝你。

mapboxgl.accessToken = 'pk.eyJ1Ijoic3ViaGFtYmFuc3BocyIsImEiOiJjajc4czNxazEyaWE5MnFwaWllNzdwdDdkIn0.6AZVCVM-wGgh5cykoII9kA'; 
 
var map = new mapboxgl.Map({ 
 
    container: 'map', // container id 
 
    style: 'mapbox://styles/mapbox/streets-v9', 
 
    center: [-80.486052, 37.830348], // starting position 
 
    zoom: 5 // starting zoom 
 
}); 
 

 

 
map.on('load',() => { 
 

 
    map.addSource("earthquakes", { 
 
    type: "geojson", 
 

 
    data: "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" 
 
    }); 
 

 
    map.addLayer({ 
 
    id: "markers", 
 
    type: "circle", 
 
    source: "earthquakes", 
 
    paint: { 
 
     "circle-color": "#11b4da", 
 
     "circle-radius": 4, 
 
     "circle-stroke-width": 1, 
 
     "circle-stroke-color": "#fff" 
 
    } 
 
    }); 
 
}); 
 

 
map.on('mouseenter', 'markers',() => { 
 
    map.getCanvas().style.cursor = 'pointer' 
 
}); 
 

 
map.on('mouseleave', 'markers',() => { 
 
    map.getCanvas().style.cursor = 'crosshair' 
 
}); 
 

 
let ground 
 
let obs 
 
map.on('contextmenu', (f) => { 
 
    ground = [f.lngLat.lng, f.lngLat.lat] 
 
    var geojson = { 
 
    "type": "FeatureCollection", 
 
    "features": [{ 
 
     "type": "Feature", 
 
     "geometry": { 
 
     "type": "Point", 
 
     "coordinates": f.lngLat 
 
     } 
 
    }] 
 
    }; 
 
    // add markers to map 
 
    geojson.features.forEach(function(marker) { 
 
    // create a DOM element for the marker 
 
    var el = document.createElement('div'); 
 
    el.className = 'marker'; 
 
    el.addEventListener('click', function() { 
 
     window.alert(f.lngLat); 
 
    }) 
 
    new mapboxgl.Marker(el) 
 
     .setLngLat(marker.geometry.coordinates) 
 
     .addTo(map); 
 

 
    map.addLayer({ 
 
     "id": "route", 
 
     "type": "line", 
 
     "source": { 
 
     "type": "geojson", 
 
     "data": { 
 
      "type": "FeatureCollection", 
 
      "features": [{ 
 
      "type": "Feature", 
 
      "geometry": { 
 
       "type": "LineString", 
 
       "coordinates": [ 
 
       ground, obs 
 
       ] 
 
      } 
 
      }, ] 
 
     } 
 
     }, 
 
     "layout": { 
 
     "line-join": "round", 
 
     "line-cap": "round" 
 
     }, 
 
     "paint": { 
 
     "line-color": "#888", 
 
     "line-width": 3, 
 
     "line-dasharray": [0.1, 1.8] 
 
     } 
 

 
    }); 
 

 
    }); 
 

 
}) 
 

 

 
map.on('click', 'markers', (e) => { 
 
    obs = [e.lngLat.lng, e.lngLat.lat] 
 
});
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#map { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    width: 100%; 
 
} 
 

 
.marker { 
 
    display: block; 
 
    border: none; 
 
    border-radius: 50%; 
 
    cursor: pointer; 
 
    padding: 0; 
 
    background-color: rgba(5, 4, 244, 0.82); 
 
    height: 10px; 
 
    width: 10px 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset='utf-8' /> 
 
    <title></title> 
 
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> 
 
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.js'></script> 
 
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.css' rel='stylesheet' /> 
 

 
</head> 
 

 
<body> 
 

 
    <div id='map'></div> 
 

 
</body> 
 

 
</html>

+0

P.S:瀏覽器控制檯提供以下內容:錯誤:已有一個具有此ID的源 –

回答

1

而是重新創建一個新層&每個用戶添加標記的時間源,您應該創建線層及其源一次,然後只更新基礎數據:

mapboxgl.accessToken = 'pk.eyJ1Ijoic3ViaGFtYmFuc3BocyIsImEiOiJjajc4czNxazEyaWE5MnFwaWllNzdwdDdkIn0.6AZVCVM-wGgh5cykoII9kA'; 
 
var map = new mapboxgl.Map({ 
 
    container: 'map', // container id 
 
    style: 'mapbox://styles/mapbox/streets-v9', 
 
    center: [-80.486052, 37.830348], // starting position 
 
    zoom: 5 // starting zoom 
 
}); 
 

 

 
map.on('load',() => { 
 

 
    map.addSource("earthquakes", { 
 
    type: "geojson", 
 
    data: "https://www.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" 
 
    }); 
 

 
    map.addLayer({ 
 
    id: "markers", 
 
    type: "circle", 
 
    source: "earthquakes", 
 
    paint: { 
 
     "circle-color": "#11b4da", 
 
     "circle-radius": 4, 
 
     "circle-stroke-width": 1, 
 
     "circle-stroke-color": "#fff" 
 
    } 
 
    }); 
 
    
 
    map.addSource('line-source', { 
 
    type: 'geojson', 
 
    data: { 
 
     type: 'FeatureCollection', 
 
     features: [] 
 
    } 
 
    }); 
 
    map.addLayer({ 
 
    type: 'line', 
 
    source: 'line-source', 
 
    id: 'line-layer' 
 
    }); 
 
}); 
 

 
map.on('mouseenter', 'markers',() => { 
 
    map.getCanvas().style.cursor = 'pointer' 
 
}); 
 

 
map.on('mouseleave', 'markers',() => { 
 
    map.getCanvas().style.cursor = 'crosshair' 
 
}); 
 

 
let ground; 
 
let obs; 
 

 
map.on('contextmenu', (f) => { 
 
    ground = [f.lngLat.lng, f.lngLat.lat]; 
 
    
 
    map.getSource('line-source').setData({ 
 
    type: 'FeatureCollection', 
 
    features: [{ 
 
     type: 'Feature', 
 
     geometry: { 
 
     type: 'LineString', 
 
     coordinates: [ground, obs] 
 
     } 
 
    }] 
 
    }) 
 
}); 
 

 

 
map.on('click', 'markers', (e) => { 
 
    obs = [e.lngLat.lng, e.lngLat.lat]; 
 
});
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 

 
#map { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    width: 100%; 
 
} 
 

 
.marker { 
 
    display: block; 
 
    border: none; 
 
    border-radius: 50%; 
 
    cursor: pointer; 
 
    padding: 0; 
 
    background-color: rgba(5, 4, 244, 0.82); 
 
    height: 10px; 
 
    width: 10px 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset='utf-8' /> 
 
    <title></title> 
 
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> 
 
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.js'></script> 
 
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.40.1/mapbox-gl.css' rel='stylesheet' /> 
 

 
</head> 
 

 
<body> 
 

 
    <div id='map'></div> 
 

 
</body> 
 

 
</html>

我簡化了你的代碼片段,但你應該明白它的要點。

+0

謝謝!是的,這看起來很整齊。我可以顯示直到最後一次右鍵單擊生成的所有行嗎?在這裏,相同ID的錯誤得到解決,但我可以看到加入最後一對點的線。如果可能的話,我希望看到所有行生成直到最後一次點擊右鍵。 @Scarysize –

+0

您可以將附加的座標添加到您正在創建的LineString幾何圖形,它只需要至少有兩個座標。 – Scarysize

+0

我將座標添加到LineString,就像這個座標:[truthArray [i],obsArray [i]],其中這兩個數組是存儲左右點的座標的數組,並且我遍歷了真數組的大小。但是,我沒有得到我想要的結果。我應該以不同的方式添加它們,以顯示左右點之間的所有線條嗎? @Scarysize –

相關問題