2017-08-31 232 views
0

所以我的想法看起來很直截了當,但我仍然努力。我想要做的事情基本上是點擊我的地圖上的任意一點,然後在主要特徵上繪製一個多邊形,即,如果我點擊了公園或建築物,則會顯示並突出顯示特定的多邊形。MapBox點擊特徵的多邊形

我用了很多這樣的代碼:的https://www.mapbox.com/mapbox-gl-js/example/queryrenderedfeatures-around-point/

但不是給它一組以GeoJSON我想我的JavaScript來選擇需要GeoJSON的數據上mousover(eventhough我不知道是否在工作一般)。現在我的代碼片斷編譯但沒有顯示任何內容。

在後面的步驟中,我想要收集同一特徵(即所有公園)的所有多邊形,並將它們顯示爲突出顯示的多邊形,然後將它們導出爲僅包含點擊該特徵的地圖表示的svg文件。也許有人也有這樣的想法?

謝謝:)方面

這是我的javascript截至目前:

//Set AccessToken from MapBox 
 
mapboxgl.accessToken = 'pk.eyJ1IjoidG1pbGRuZXIiLCJhIjoiY2o1NmlmNWVnMG5rNzMzcjB5bnV3YTlnbiJ9.r0BCga0qhRaHh0CnDdcGBQ'; 
 

 
//Setup starting view point at Uni-Bremen campus 
 
var map = new mapboxgl.Map({ 
 
\t container: 'content-map', 
 
    style: 'mapbox://styles/mapbox/streets-v9', 
 
    center: [8.85307, 53.10810], 
 
    zoom: 16 
 
}); 
 

 

 
//Add a search bar -> hidden for presentation 
 
/*map.addControl(new MapboxGeocoder({ 
 
    accessToken: mapboxgl.accessToken 
 
}));*/ 
 

 
//Function to show all Features of a certian point 
 
map.on('mousemove', function (e) { 
 
    var features = map.queryRenderedFeatures(e.point); 
 
    document.getElementById('features').innerHTML = JSON.stringify(features, null, 2); 
 
    console.log(JSON.stringify(features, null, 2)); 
 

 
    drawPolygon(); 
 
}); 
 

 

 

 
//Draw a Polygon 
 
function drawPolygon() { 
 

 
\t //set boundary box as 5px rectangle area around clicked point 
 
\t var bbox = [[e.point.x - 5, e.point.y - 5], [e.pont.x + 5, e.point.y + 5]]; 
 

 
\t //set the data on pointer using the bbox 
 
\t var data = map.queryRenderedFeatures(bbox); 
 

 
\t map.on('load', function() { 
 
\t \t 
 
\t \t var dataSource = 'school'; 
 

 
\t \t //set school to the feature and use 'setJsonData' as data source. 
 
\t \t map.addSource(dataSource, { 
 
\t \t \t 'type': 'geojson', 
 
\t \t \t 'data': data 
 
\t \t }); 
 
\t \t //adding a new layer for the general display 
 
\t \t map.addLayer({ 
 
\t \t \t 'id': 'dataSet', 
 
\t \t \t 'type': 'fill', 
 
\t \t \t 'source': dataSource, 
 
\t \t \t 'source-layer': 'original', 
 
\t \t \t 'paint': { 
 
\t \t \t \t 'fill-outline-color': 'rgba(0,0,0,0.1)', 
 
\t \t \t \t 'fill-color': 'rgba(0,0,0,0.1)' 
 
\t \t \t } 
 
\t \t }, 'place-city-sm'); //place polygon under these labels 
 

 
\t \t 
 
\t \t //adding a new layer for the polygon to be drawn 
 
\t \t map.addLeyer({ 
 
\t \t \t 'id': 'dataSet-highlighted', 
 
\t \t \t 'type': 'fill', 
 
\t \t \t 'source': dataSource, 
 
\t \t \t 'source-layer': 'original', 
 
\t \t \t 'paint': { 
 
      \t 'fill-outline-color': '#484896', 
 
      \t \t 'fill-color': '#6e599f', 
 
      \t 'fill-opacity': 0.75 
 
      }, 
 
      'filter': ['in', 'FIPS', ''] 
 
\t \t }, 'place-city-sm'); //place polygon under these labels 
 

 
\t \t 
 
\t \t //action on click to show the polygon and change their color 
 
\t \t map.on('click', function (e) { 
 
\t \t \t 
 
\t \t \t //retrieve data from 'dataSource' 
 
\t \t \t var dataFromSource = map.queryRenderedFeatures(bbox, {layers: ['dataSource'] }); 
 

 

 
\t \t \t // Run through the selected features and set a filter 
 
     \t // to match features with unique FIPS codes to activate 
 
     \t \t // the `counties-highlighted` layer. 
 
\t \t \t var filter = dataSource.reduce(function(memo, dataSource) { 
 
\t \t \t \t memo.push(dataSource, properties.FIPS); 
 
\t \t \t \t return memo; 
 
\t \t \t } ['in', 'FIPS']); 
 
\t \t \t 
 
\t \t \t map.setFilter('dataSet-highlighted', filter); 
 
\t \t }); 
 

 

 
\t }); 
 
}

+0

我只是要添加我爲此創建的codepen,可能有助於查看我做了什麼 - > https://codepen.io/callmethomas/pen/RZqXzv –

回答

0

我不是100%肯定你問什麼,但我的解釋是當你將鼠標懸停在某些類型的幾何體上時,你想特別設計某種類型的幾何體,例如「公園」。你在正確的道路上,使用map.queryRenderedFeatures()是偉大的。我使用了相同的Mapbox Streets樣式的示例,該樣式僅查詢building圖層,並在鼠標懸停上查找university類型。

當交互遇到適當的功能時,它使用新功能更新源數據,然後更新school-hover層。

退房筆在這裏:https://codepen.io/mapsam/pen/oemqKb

在後面的步驟我想收集相同特徵的所有多邊形,即所有的公園,並將其顯示爲高亮顯示的多邊形,然後將其導出爲一個SVG文件其中只包含點擊的功能的地圖表示。

我不會去到文件出口,但是要記住,所有的結果返回從map.queryRenderedFeatures是特定於要查詢的單載體瓦,這可能會導致在小塊邊界的問題,其中一個多邊形不完全由您當前的查詢覆蓋。

看看這個example where we are highlighting features with similar data,它應該允許你得到所有必要的幾何圖形並導出到SVG。

乾杯!

+0

非常感謝!那幫了我很多:) –