Openlayers繪制聚合標(biāo)注
本文實(shí)例為大家分享了Openlayers實(shí)現(xiàn)聚合標(biāo)注的具體代碼,供大家參考,具體內(nèi)容如下
1、聚合標(biāo)注
聚合標(biāo)注是指在不同的地圖分辨率下,通過聚合的方式來展示標(biāo)注點(diǎn)的一種方法,其目的就是為了減少當(dāng)前視窗中加載的標(biāo)注點(diǎn)的數(shù)量,從而提高客戶端的渲染速度,有點(diǎn)類似于ArcGIS的點(diǎn)抽稀。
2、代碼實(shí)現(xiàn)
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="../lib/ol/ol.js"></script> <script type="text/javascript"> window.onload = function () { //初始化地圖 var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: new ol.proj.fromLonLat([116.28, 39.54]), zoom: 8 }) }); //創(chuàng)建要素的數(shù)量 //10000個(gè)點(diǎn)沒有任何壓力,50000個(gè)點(diǎn)稍微有些卡了,100000個(gè)點(diǎn)可以把瀏覽器卡崩潰 var count = 10000; //創(chuàng)建一個(gè)要素?cái)?shù)組 var features = new Array(count); //坐標(biāo)偏移量 var e = 8500000; for (var i = 0; i < count; i++) { //要素坐標(biāo) var coordinates = [3 * e * Math.random() - e, 2 * e * Math.random() - e]; //新建點(diǎn)要素 features[i] = new ol.Feature(new ol.geom.Point(coordinates)); } //初始化矢量數(shù)據(jù)源 var source = new ol.source.Vector({ //要素 features:features }); //初始化聚合標(biāo)注數(shù)據(jù)源 var clusterSource = new ol.source.Cluster({ //標(biāo)注元素之間的間距 distance: 40, //數(shù)據(jù)源 source:source }); //樣式緩存 var styleCache = {}; //初始化矢量圖層 var clusters = new ol.layer.Vector({ //數(shù)據(jù)源 source: clusterSource, //樣式 style: function (feature, resolution) { //當(dāng)前聚合標(biāo)注數(shù)據(jù)源的要素大小 var size = feature.get('features').length; //定義樣式 var style = styleCache[size]; //如果當(dāng)前樣式不存在則創(chuàng)建 if (!style) { style = [ //初始化樣式 new ol.style.Style({ //點(diǎn)樣式 image: new ol.style.Circle({ //點(diǎn)的半徑 radius: 10, //筆觸 stroke: new ol.style.Stroke({ color: '#fff' }), //填充 fill: new ol.style.Fill({ color: '#3399cc' }) }), //文本樣式 text: new ol.style.Text({ //文本內(nèi)容 text: size.toString(), //填充 fill: new ol.style.Fill({ color: '#fff' }) }) }) ]; styleCache[size] = style; } return style; } }); //將聚合標(biāo)注圖層添加到map中 map.addLayer(clusters); //獲取添加聚合標(biāo)注按鈕 document.getElementById('addFeatures').onclick = function () { //獲取聚合標(biāo)注數(shù)據(jù)源中的要素 var currentFeatures = clusterSource.getSource().getFeatures(); //如果當(dāng)前數(shù)據(jù)源中沒有任何要素則添加 if (currentFeatures.length == 0) { clusterSource.getSource().addFeatures(features); clusters.setSource(clusterSource); map.addLayer(clusters); } }; //獲取移除聚合標(biāo)注的按鈕 document.getElementById('removeFeatures').onclick = function () { //清除聚合標(biāo)注數(shù)據(jù)源中的所有元素 clusterSource.getSource().clear(); //從map中移除聚合標(biāo)注圖層 map.removeLayer(clusters); }; }; </script> </head> <body> <input type="button" name="name" value="添加聚合標(biāo)簽" id="addFeatures" /> <input type="button" name="name" value="移除聚合標(biāo)簽" id="removeFeatures" /> <div id="map"></div> </body> </html>
3、結(jié)果展示
初始化界面
隨意更改地圖的分辨率(進(jìn)行縮放操作),標(biāo)注點(diǎn)的數(shù)量也會跟著改變
單擊左上角的移除聚合標(biāo)簽按鈕,則會清空界面上的所有標(biāo)注
單擊左上角的添加聚合標(biāo)簽按鈕,則會在界面上重新添加聚合標(biāo)注
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過實(shí)例解析JavaScript for in及for of區(qū)別
這篇文章主要介紹了通過實(shí)例解析JavaScript for in及for of區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06JS響應(yīng)鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)兩個(gè)滑塊區(qū)間拖動效果
這篇文章主要介紹了JS實(shí)現(xiàn)的兩個(gè)滑塊區(qū)間拖動效果代碼,涉及JavaScript響應(yīng)鼠標(biāo)事件針對頁面元素的動態(tài)操作技巧,需要的朋友可以參考下2015-10-10詳解js產(chǎn)生對象的3種基本方式(工廠模式,構(gòu)造函數(shù)模式,原型模式)
本篇文章主要介紹了js產(chǎn)生對象的3種基本方式(工廠模式,構(gòu)造函數(shù)模式,原型模式) ,具有一定的參考價(jià)值,有興趣的可以了解一下2017-01-01JavaScript中string轉(zhuǎn)換成number介紹
這篇文章主要介紹了JavaScript中string轉(zhuǎn)換成number介紹,本文講解了3種將string值轉(zhuǎn)換成number的方法,需要的朋友可以參考下2014-12-12JavaScript通過元素索引號刪除數(shù)組中對應(yīng)元素的方法
這篇文章主要介紹了JavaScript通過元素索引號刪除數(shù)組中對應(yīng)元素的方法,涉及javascript操作數(shù)組的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03element-table表格中插入顏色塊顯示數(shù)據(jù)狀態(tài)的示例代碼
這篇文章主要介紹了element-table表格中插入顏色塊顯示數(shù)據(jù)狀態(tài),代碼部分分為dom部分和data部分及css部分,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-12-12

使用JS模擬錨點(diǎn)跳轉(zhuǎn)的實(shí)例