|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>Graphic图层符号绘制</title> |
| 6 | + <style type="text/css"> |
| 7 | + body{ |
| 8 | + margin: 0; |
| 9 | + overflow: hidden; |
| 10 | + background: #fff; |
| 11 | + } |
| 12 | + #map{ |
| 13 | + position: relative; |
| 14 | + height: 500px; |
| 15 | + border:1px solid #3473b7; |
| 16 | + } |
| 17 | + #toolbar { |
| 18 | + position: relative; |
| 19 | + padding-top: 5px; |
| 20 | + padding-bottom: 10px; |
| 21 | + } |
| 22 | + </style> |
| 23 | + |
| 24 | + <script type="text/javascript"> |
| 25 | + var map,layer, graphicLayer, symbolinfo, selectGraphic, popup = null,img,imageWidth=32, imagePaths = ['./images/marker_blue.png','./images/marker_red.png'],imageIdx = 0, |
| 26 | + host = document.location.toString().match(/file:\/\//) ? "http://support.supermap.com.cn:8090" : 'http://' + document.location.host; |
| 27 | + url = host + "/iserver/services/map-china400/rest/maps/China"; |
| 28 | + function init() |
| 29 | + { |
| 30 | + |
| 31 | + if(!document.createElement("canvas").getContext){ |
| 32 | + alert("您的浏览器不支持Canvas,请先升级"); |
| 33 | + return; |
| 34 | + } |
| 35 | + map = new SuperMap.Map("map",{controls:[ |
| 36 | + new SuperMap.Control.Zoom(), |
| 37 | + new SuperMap.Control.Navigation() , |
| 38 | + new SuperMap.Control.LayerSwitcher() |
| 39 | + ]}); |
| 40 | + layer= new SuperMap.Layer.TiledDynamicRESTLayer("World", url, null,{maxResolution:"auto"}); |
| 41 | + layer.events.on({"layerInitialized":addLayer}); |
| 42 | + graphicLayer = new SuperMap.Layer.Graphics("Graphic Layer", null, {hitDetection: true, useCanvasEvent: false}); |
| 43 | + |
| 44 | + selectGraphic = new SuperMap.Control.SelectGraphic(graphicLayer, { |
| 45 | + onSelect: onGraphicSelect, |
| 46 | + hover: false, |
| 47 | + onUnSelect: onUnGraphicSelect |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + function onGraphicSelect(result, evt) { |
| 52 | + var image = graphicLayer.style.image; |
| 53 | + |
| 54 | + var pixel = map.getPixelFromLonLat(new SuperMap.LonLat(result.geometry.x, result.geometry.y)); |
| 55 | + var evtPixel = evt.xy; |
| 56 | + //点击点与中心点的角度 |
| 57 | + var angle = (Math.atan2(evtPixel.y - pixel.y, evtPixel.x - pixel.x))/Math.PI*180; |
| 58 | + angle = angle > 0 ? angle : 360+angle; |
| 59 | + //确定扇叶 |
| 60 | + var index = Math.ceil(angle/(image.angle + image.spaceAngle)); |
| 61 | + addPopup(result, evt, index); |
| 62 | + } |
| 63 | + function onUnGraphicSelect(evt) { |
| 64 | + //alert(2); |
| 65 | + } |
| 66 | + |
| 67 | + function addPopup(feature, evt, index) { |
| 68 | + if(!!popup) { |
| 69 | + map.removePopup(popup); |
| 70 | + } |
| 71 | + selectedFeature = feature; |
| 72 | + var lonlat = new SuperMap.LonLat(feature.geometry.x,feature.geometry.y); |
| 73 | + var contentHTML = "<div style='font-size:.8em; opacity: 0.8; overflow-y:hidden;'>" + |
| 74 | + "<span style='font-weight: bold; font-size: 12px;'>要素ID: " + feature.id + "</span><br>"; |
| 75 | + if(symbolinfo instanceof SuperMap.Style.Clover){ |
| 76 | + contentHTML +="<span style='font-weight: bold; font-size: 12px;'>叶数索引: " + index + "</span><br>"; |
| 77 | + lonlat = map.getLonLatFromPixel(evt.xy); |
| 78 | + }else{ |
| 79 | + lonlat = new SuperMap.LonLat(feature.geometry.x,feature.geometry.y); |
| 80 | + } |
| 81 | + contentHTML +="</div>" |
| 82 | + //初始化一个弹出窗口,当某个地图要素被选中时会弹出此窗口,用来显示选中地图要素的属性信息 |
| 83 | + popup = new SuperMap.Popup.FramedCloud("chicken", |
| 84 | + lonlat, |
| 85 | + null, |
| 86 | + contentHTML, |
| 87 | + null, |
| 88 | + true); |
| 89 | + feature.popup = popup; |
| 90 | + map.addPopup(popup); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + function addLayer(){ |
| 95 | + map.addLayers([layer,graphicLayer]); |
| 96 | + //显示地图范围 |
| 97 | + map.setCenter(new SuperMap.LonLat(0, 0), 1); |
| 98 | + |
| 99 | + map.addControl(selectGraphic); |
| 100 | + selectGraphic.activate(); |
| 101 | + addClover(); |
| 102 | + } |
| 103 | + |
| 104 | + //symbol相关属性 填充色、边框颜色、半径、 |
| 105 | + var fillColors = 'rgba(255,153,0,1)'; |
| 106 | + var strokeColors = 'rgba(255,204,0,1)'; |
| 107 | + var radius = 9; |
| 108 | + |
| 109 | + |
| 110 | + var e = 10000000; |
| 111 | + function addClover() { |
| 112 | + symbolinfo = new SuperMap.Style.Clover({ |
| 113 | + radius: radius, |
| 114 | + fill: new SuperMap.Style.Fill({ |
| 115 | + color: fillColors |
| 116 | + }), |
| 117 | + stroke: new SuperMap.Style.Stroke({ |
| 118 | + color: strokeColors |
| 119 | + }) |
| 120 | + }); |
| 121 | + loadData(); |
| 122 | + } |
| 123 | + function addData(img) |
| 124 | + { |
| 125 | + if(img.complete ){ |
| 126 | + loadData(); |
| 127 | + }else{ |
| 128 | + img.onload = function() { |
| 129 | + loadData(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + function loadData() { |
| 135 | + if(!!popup) { |
| 136 | + map.removePopup(popup); |
| 137 | + } |
| 138 | + graphicLayer.removeAllGraphics(); |
| 139 | + var total = document.getElementById("total").value; |
| 140 | + graphicLayer.style = { |
| 141 | + image: symbolinfo |
| 142 | + }; |
| 143 | + var points = []; |
| 144 | + for(var i = 0; i<total; i++){ |
| 145 | + var point= new SuperMap.Geometry.Point(2 * e * Math.random() - e, 2 * e * Math.random() - e); |
| 146 | + var pointVector = new SuperMap.Graphic(point); |
| 147 | + //pointVector.style = ; |
| 148 | + points.push(pointVector) |
| 149 | + } |
| 150 | + graphicLayer.addGraphics(points); |
| 151 | + } |
| 152 | + |
| 153 | + function redraw() { |
| 154 | + var idx = (++imageIdx)%2; |
| 155 | + img = new Image(); |
| 156 | + img.src = imagePaths[idx]; |
| 157 | + symbolinfo = new SuperMap.Style.Image({ |
| 158 | + img:img, |
| 159 | + anchor:[16,16] |
| 160 | + }); |
| 161 | + graphicLayer.style = { |
| 162 | + image: symbolinfo |
| 163 | + }; |
| 164 | + if(img.complete ){ |
| 165 | + graphicLayer.redraw(); |
| 166 | + }else{ |
| 167 | + img.onload = function() { |
| 168 | + graphicLayer.redraw(); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + //移除数据 |
| 175 | + function removeData() |
| 176 | + { |
| 177 | + if(!!popup) { |
| 178 | + map.removePopup(popup); |
| 179 | + } |
| 180 | + graphicLayer.removeAllGraphics(); |
| 181 | + graphicLayer.refresh(); |
| 182 | + } |
| 183 | + function plus() { |
| 184 | + var idx = (++imageIdx)%2; |
| 185 | + img = new Image(); |
| 186 | + imageWidth +=10; |
| 187 | + img.src = imagePaths[idx]; |
| 188 | + symbolinfo = new SuperMap.Style.Image({ |
| 189 | + img:img, |
| 190 | + anchor:[imageWidth/2,imageWidth/2] |
| 191 | + }); |
| 192 | + graphicLayer.style = { |
| 193 | + image: symbolinfo, |
| 194 | + graphicHeight:imageWidth, |
| 195 | + graphicWidth:imageWidth |
| 196 | + }; |
| 197 | + if(img.complete ){ |
| 198 | + graphicLayer.redraw(); |
| 199 | + }else{ |
| 200 | + img.onload = function() { |
| 201 | + graphicLayer.redraw(); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + function minus() { |
| 206 | + var idx = (++imageIdx)%2; |
| 207 | + img = new Image(); |
| 208 | + imageWidth -=10; |
| 209 | + if(imageWidth < 5){ |
| 210 | + imageWidth = 5; |
| 211 | + } |
| 212 | + img.src = imagePaths[idx]; |
| 213 | + symbolinfo = new SuperMap.Style.Image({ |
| 214 | + img:img, |
| 215 | + anchor:[imageWidth/2,imageWidth/2] |
| 216 | + }); |
| 217 | + graphicLayer.style = { |
| 218 | + image: symbolinfo, |
| 219 | + graphicHeight:imageWidth, |
| 220 | + graphicWidth:imageWidth |
| 221 | + }; |
| 222 | + if(img.complete ){ |
| 223 | + graphicLayer.redraw(); |
| 224 | + }else{ |
| 225 | + img.onload = function() { |
| 226 | + graphicLayer.redraw(); |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + </script> |
| 231 | +</head> |
| 232 | +<body onload="init()" > |
| 233 | +<div id="toolbar"> |
| 234 | + <input id="total" type="text" style="width: 100px" value="100000"> |
| 235 | + <input type="button" class="btn" style="margin-bottom: 10px" value="开始绘制" onclick="addClover()"> |
| 236 | + <input type="button" class="btn" style="margin-bottom: 10px" value="切换图标并重绘" onclick="redraw()"> |
| 237 | + <input type="button" class="btn" style="margin-bottom: 10px" value="+" onclick="plus()"> |
| 238 | + <input type="button" class="btn" style="margin-bottom: 10px" value="-" onclick="minus()"> |
| 239 | + <input type="button" class="btn" style="margin-bottom: 10px" value="移除" onclick="removeData()"> |
| 240 | + |
| 241 | +</div> |
| 242 | +<div id="map"></div> |
| 243 | +<script src = '../libs/SuperMap.Include.js'></script> |
| 244 | +</body> |
| 245 | +</html> |
0 commit comments