mapbox-gl 使用turf計算距離(代碼片段)

參考:geojson對象的說明:(以後添加)

HTML DOM CURSOR :  https://www.w3school.com.cn/jsref/prop_style_cursor.asp

基本思路是:

1.定義geojson對象,包括point和linestring;

2.map.on就啓動回調函數,回調函數做以下幾件事:

2.1  添加數據源,添加圖層

2.2  監聽鼠標點擊事件

2.3  如果點擊在點圖層,進行刪除,如果不是,那麼就是添加;

2.4  添加點、添加線

2.5 對數據源添加數據。

直接上代碼,註釋即解釋。


//顯示計算結果的div
var distanceContainer = document.getElementById('distance');
// GeoJSON object to hold our measurement features
var geojson = {
	'type': 'FeatureCollection',
	'features': []
};
// Used to draw a line between points
var linestring = {
	'type': 'Feature',
	'geometry': {
		'type': 'LineString',
		'coordinates': []
	}
};

map.on('load', function() {
	map.addSource('geojson', {
		'type': 'geojson',
		'data': geojson
	}); 
	// Add styles to the map
	map.addLayer({
		id: 'measure-points',
		type: 'circle',
		source: 'geojson',
		paint: {
			'circle-radius': 5,
			'circle-color': '#000'
		},
		filter: ['in', '$type', 'Point']
	});
	map.addLayer({
		id: 'measure-lines',
		type: 'line',
		source: 'geojson',
		layout: {
			'line-cap': 'round',
			'line-join': 'round'
		},
		paint: {
			'line-color': '#000',
			'line-width': 2.5
		},
		filter: ['in', '$type', 'LineString']
	});
 
	map.on('click', function(e) {
		//獲取鼠標所在經緯度的圖層的屬性
		var features = map.queryRenderedFeatures(e.point, {
			layers: ['measure-points']
		});
 
		// Remove the linestring from the group
		// So we can redraw it based on the points collection
		if (geojson.features.length > 1)  geojson.features.pop();
	 
		// Clear the Distance container to populate it with a new value
		distanceContainer.innerHTML = '';
	 
		// If a feature was clicked, remove it from the map
		if (features.length) {
			var id = features[0].properties.id;
			//geojson的過濾器,哪裏可以找到它的相關規範說明
			geojson.features = geojson.features.filter(function(point) {
				return point.properties.id !== id;
			});
		} else {
			var point = {
				'type': 'Feature',
				'geometry': {
					'type': 'Point',
					'coordinates': [e.lngLat.lng, e.lngLat.lat]
				},
				'properties': {
					'id': String(new Date().getTime())
				}
			}; 
			//畫點
			geojson.features.push(point);
		}
	 
		if (geojson.features.length > 1) {
			linestring.geometry.coordinates = geojson.features.map(function(point) {
				return point.geometry.coordinates;
			});	
			//畫線
			geojson.features.push(linestring);
			 
			// Populate the distanceContainer with total distance
			var value = document.createElement('pre');
			value.textContent =
				'Total distance: ' +
				turf.length(linestring).toLocaleString() +
				'km';
			distanceContainer.appendChild(value);
		}		 
		map.getSource('geojson').setData(geojson);
	});
});
	 
map.on('mousemove', function(e) {
	//獲取鼠標所在經緯度的圖層的屬性
	var features = map.queryRenderedFeatures(e.point, {
		layers: ['measure-points']
	});
	// UI indicator for clicking/hovering a point on the map
	// style.cursor 是HTML DOM的 鼠標對象
	map.getCanvas().style.cursor = features.length
		? 'pointer'
		: 'crosshair';
});
	

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章