1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" /> 7 <script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script> 8 <title>Document</title> 9 </head> 10 <body> 11 <div id="map"></div> 12 <div id="selectedFeatures"></div> 13 <script> 14 var beijing = ol.proj.fromLonLat([116.28, 39.54]); 15 var map = new ol.Map({ 16 target: 'map', 17 layers: [ 18 new ol.layer.Tile({ 19 source: new ol.source.OSM() 20 }) 21 ], 22 view: new ol.View({ 23 center: beijing, 24 zoom: 4 25 }) 26 }); 27 28 //實例化矢量點要素,通過矢量圖層添加到地圖容器中 29 //這樣就實現了預先加載圖文標注 30 var iconFeature = new ol.Feature({ 31 geometry: new ol.geom.Point(beijing), 32 name: '北京市', //名稱屬性 33 population: 2115 //人口數(萬) 34 }); 35 //設置點要素樣式 36 iconFeature.setStyle(createLabelStyle(iconFeature)); 37 //矢量標注的數據源 38 var vectorSource = new ol.source.Vector({ 39 features: [iconFeature] 40 }); 41 //矢量標注圖層 42 var vectorLayer = new ol.layer.Vector({ 43 source: vectorSource 44 }); 45 map.addLayer(vectorLayer); 46 47 //矢量標注樣式設置函數,設置image為圖標ol.style.Icon 48 function createLabelStyle(feature){ 49 // console.log(feature); 50 return new ol.style.Style({ 51 image: new ol.style.Icon({ 52 anchor: [0.5, 60], //錨點 53 anchorOrigin:'top-right', //錨點源 54 anchorXUnits: 'fraction', //錨點X值單位 55 anchorYUnits: 'pixels', //錨點Y值單位 56 offsetOrigin: 'top-right', //偏移原點 57 opacity: 0.75, 58 scale: 0.05, 59 src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594359193211&di=1d6bb819a5daa724ff65cc4d011d4d42&imgtype=0&src=http%3A%2F%2Fku.90sjimg.com%2Felement_pic%2F17%2F10%2F27%2F05dc60e54e3aa5d093cdc32611303643.jpg' //圖標的URL 60 }), 61 text: new ol.style.Text({ 62 textAlign: 'center', //位置 63 textBaseline: 'bottom', //基准線 64 font: 'normal 12px 微軟雅黑', //文字樣式 65 text: feature.get('name'), //文本內容 66 fill: new ol.style.Fill({ //文本填充樣式(即文字顏色) 67 color: '#000' 68 }), 69 stroke: new ol.style.Stroke({ 70 color: '#F00', 71 width: 2 72 }) 73 }) 74 }); 75 } 76 var coordinate1 = [10806361.310845079, 3942927.667062532]; //鼠標單擊點的坐標 77 var coordinate2 = [11540156.782382771, 4539747.983913189] //鼠標單擊點的坐標 78 var coordinate3 = [12225032.55581795, 3982063.4255445423] //鼠標單擊點的坐標 79 var arr =[coordinate1,coordinate2,coordinate3] 80 //新建一個要素ol.Feature 81 arr.forEach((ar,index) => { 82 var newFeature = new ol.Feature({ 83 geometry: new ol.geom.Point(ar), //幾何信息 84 name: '標注點'+index 85 }); 86 newFeature.setStyle(createLabelStyle(newFeature)); //設置要素樣式 87 vectorSource.addFeature(newFeature); 88 }); 89 90 var draw = new ol.interaction.Draw({ 91 source: vectorLayer.getSource(), 92 type:"Circle", 93 style:new ol.style.Style({ 94 // 將點設置成圓形樣式 95 image: new ol.style.Circle({ 96 // 點的顏色 97 fill: new ol.style.Fill({ 98 color: '#F00' 99 }), 100 // 圓形半徑 101 radius: 5 102 }), 103 // 線樣式 104 stroke: new ol.style.Stroke({ 105 color: '#0F0', 106 lineCap: 'round', // 設置線的兩端為圓頭 107 width: 5 108 }) 109 }), 110 geometryFunction: ol.interaction.Draw.createBox() // 使畫出來的現狀為矩形 111 112 }); 113 map.addInteraction(draw); 114 draw.on('drawend',function(evt){ 115 var polygon = evt.feature.getGeometry(); 116 setTimeout(function(){ //如果不設置延遲,范圍內要素選中后自動取消選中,具體原因不知道 117 var extent = polygon.getExtent() 118 var features = vectorLayer.getSource().getFeaturesInExtent(extent); //先縮小feature的范圍 119 var str = ""; 120 for(var i=0;i<features.length;i++){ 121 var newCoords = features[i].getGeometry().getCoordinates(); 122 if (features[i].get("name")) { 123 str += "<div class=\"selectedItem\" οnclick='showDeviceOnMap(\""+features[i].getId()+"\");'>"+features[i].get("name")+"</div>"; 124 } 125 } 126 document.getElementById('selectedFeatures').innerHTML = str 127 },300) 128 }) 129 </script> 130 </body> 131 </html>
原創:https://www.cnblogs.com/wwj007/p/14029494.html
