|
15 | 15 | height: 553px;
|
16 | 16 | border: 1px solid #3473b7;
|
17 | 17 | }
|
| 18 | + #layerItems{ |
| 19 | + background-color: white; |
| 20 | + color: #0083cb; |
| 21 | + padding-left: 0; |
| 22 | + border: 2px solid #0083cb; |
| 23 | + } |
| 24 | + #layerItems:before{ |
| 25 | + content: '请选择图层:'; |
| 26 | + background-color: #0083cb; |
| 27 | + color: white; |
| 28 | + list-style: none; |
| 29 | + padding-top: 1px; |
| 30 | + font-size: 19px; |
| 31 | + } |
| 32 | + #layerItems li{ |
| 33 | + list-style: none; |
| 34 | + padding: 5px; |
| 35 | + font-size: 17px; |
| 36 | + } |
| 37 | + #layerItems li:hover{ |
| 38 | + background-color: #0083cb; |
| 39 | + color: white; |
| 40 | + } |
18 | 41 | </style>
|
19 |
| - <script src='../libs/SuperMap.Include.js'></script> |
20 | 42 | <script type="text" id="textData">
|
21 | 43 | @color:#ee9900;
|
22 | 44 | *{
|
|
29 | 51 | }
|
30 | 52 | </script>
|
31 | 53 | <script type="text/javascript">
|
32 |
| - var map, layer, lat, lon, geolocate, infowin,lonLat,geometryInfo, host = document.location.toString().match(/file:\/\//) ? "http://localhost:8090" : 'http://' + document.location.host; |
| 54 | + var map, layer, lat, lon, geolocate, layerItems,infowin,lonLat,geometryInfo,featureInfoes, host = document.location.toString().match(/file:\/\//) ? "http://localhost:8090" : 'http://' + document.location.host; |
33 | 55 | url = host + "/iserver/services/map-china400/rest/maps/China";
|
34 | 56 |
|
35 | 57 | function init() {
|
36 | 58 | if(!document.createElement('canvas').getContext) {
|
37 | 59 | alert('您的浏览器不支持 canvas,请升级');
|
38 | 60 | return;
|
39 | 61 | }
|
40 |
| - |
| 62 | + layerItems = document.getElementById('layerItems'); |
41 | 63 | map = new SuperMap.Map("map", {controls: [
|
42 | 64 | new SuperMap.Control.ScaleLine(),
|
43 | 65 | new SuperMap.Control.Zoom(),
|
|
53 | 75 | layer.events.on({"layerInitialized": addLayer});
|
54 | 76 |
|
55 | 77 | map.events.on({'click':function(evt){
|
56 |
| - var featureInfo = layer.getFeature(evt.xy.x,evt.xy.y); |
57 |
| - var lonlat = map.getLonLatFromViewPortPx(evt.xy); |
58 |
| - openPopup(featureInfo.feature,lonlat); |
59 |
| - layer.highlightFeatures(featureInfo); |
| 78 | + closeInfoWin(); |
| 79 | + layer.unHightlightFeatures(); |
| 80 | + featureInfoes = layer.getFeature(evt.xy.x,evt.xy.y); |
| 81 | + console.log(featureInfoes); |
| 82 | + if(featureInfoes && featureInfoes.length > 0){ |
| 83 | + while (layerItems.firstChild) { |
| 84 | + layerItems.removeChild(layerItems.firstChild); |
| 85 | + } |
| 86 | + layerItems.style.top = evt.clientY + 'px'; |
| 87 | + layerItems.style.left = evt.clientX + 'px'; |
| 88 | + for(var i=0,len=featureInfoes.length;i<len;i++){ |
| 89 | + var li = document.createElement('li'); |
| 90 | + li.innerHTML = featureInfoes[i].cartoLayer.layerName; |
| 91 | + li.setAttribute('data-index',i); |
| 92 | + layerItems.appendChild(li); |
| 93 | + if(i !== (len-1)){ |
| 94 | + li.style.borderBottom = '1px solid'; |
| 95 | + } |
| 96 | + li.onclick = liClickHandle; |
| 97 | + } |
| 98 | + layerItems.style.display = 'block'; |
| 99 | + }else{ |
| 100 | + layerItems.style.display = 'none'; |
| 101 | + } |
| 102 | + |
| 103 | + }, |
| 104 | + 'rightclick':function(){ |
| 105 | + layerItems.style.display = 'none'; |
| 106 | + }, |
| 107 | + 'move':function(){ |
| 108 | + layerItems.style.display = 'none'; |
60 | 109 | }});
|
61 | 110 | }
|
62 | 111 |
|
| 112 | + function liClickHandle(evt){ |
| 113 | + var evt = window.event ||evt; |
| 114 | + var target = evt.srcElement || evt.target; |
| 115 | + evt.stopPropagation(); |
| 116 | + var index = +target.dataset.index; |
| 117 | + layer.highlightFeatures(featureInfoes[index]); |
| 118 | + var lonlat = map.getLonLatFromViewPortPx(featureInfoes.xy); |
| 119 | + openPopup(featureInfoes[index].feature,lonlat); |
| 120 | + layerItems.style.display = 'none'; |
| 121 | + } |
| 122 | + |
63 | 123 |
|
64 | 124 | function addLayer() {
|
65 | 125 | map.addLayers([layer]);
|
|
69 | 129 |
|
70 | 130 | //定义mouseClickHandler函数,触发click事件会调用此函数
|
71 | 131 | function openPopup(feature,lonlat){
|
72 |
| - closeInfoWin(); |
| 132 | + var key = 'NAME'; |
| 133 | + var val = feature && feature.attributes && feature.attributes[key]; |
| 134 | + if(!val){ |
| 135 | + key = 'SmID'; |
| 136 | + val = feature && feature.attributes && feature.attributes[key]; |
| 137 | + } |
73 | 138 | var contentHTML = "<div style='width:80px; font-size:12px;font-weight:bold ; opacity: 0.8'>";
|
74 |
| - contentHTML += "面积为:"+ feature.attributes.SmArea; |
| 139 | + contentHTML += key+":"+ val; |
75 | 140 | contentHTML += "</div>";
|
76 | 141 |
|
77 | 142 | //初始化FramedCloud类
|
|
106 | 171 | <body onload="init()">
|
107 | 172 | <div id="map"></div>
|
108 | 173 | <button onclick="layer.unHightlightFeatures();closeInfoWin();">取消高亮</button>
|
| 174 | +<ul id='layerItems' style="display: none;position: absolute;"> |
| 175 | + |
| 176 | +</ul> |
| 177 | +<script src='../libs/SuperMap.Include.js'></script> |
109 | 178 | </body>
|
110 | 179 | </html>
|
0 commit comments