123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- <!--********************************************************************
- * Copyright© 2000 - 2021 SuperMap Software Co.Ltd. All rights reserved.
- *********************************************************************-->
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title data-i18n="resources.title_cartoCSSHightlight"></title>
- <style type="text/css">
- body {
- margin: 0;
- overflow: hidden;
- background: #fff;
- width: 100%;
- height: 100%
- }
- #map {
- position: absolute;
- width: 100%;
- height: 100%;
- }
- #toolbar {
- position: absolute;
- top: 50px;
- right: 10px;
- text-align: center;
- z-index: 100;
- border-radius: 4px;
- }
- .layerItems {
- border: 1px solid #999;
- display: none;
- position: absolute;
- z-index: 1000;
- background-color: rgba(255,255,255,0.5);
- padding-right: 20px;
- }
- </style>
- </head>
- <body>
- <div id="toolbar" class="panel panel-primary">
- <div class='panel-heading'>
- <h5 class='panel-title text-center' data-i18n="resources.text_cartoCSSHightlight"></h5></div>
- <div class='panel-body content'>
- <input type="button" class="btn btn-default" data-i18n="[value]resources.btn_cancelHighlight"
- onclick="closeInfoWin();"/>
- </div>
- </div>
- <div class="layerItems">选择需要高亮的要素:
- <ul id='layerItems'></ul>
- </div>
- <div id="map"></div>
- <script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
- <script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
- <script type="text" id="textData">
- @color:#ee9900;
- *{
- line-color:@color;
- line-opacity:1;
- line-width:3;
- polygon-fill:@color;
- polygon-opacity:0.4;
- text-fill:#000000;
- }
- </script>
- <script>
- var map, layer, layerItems,layerItemsContainer, infowin, featureInfoes,
- host = window.isLocal ? window.server : "https://iserver.supermap.io",
- url = host + "/iserver/services/map-china400/rest/maps/China";
- init();
- function init() {
- if (!document.createElement('canvas').getContext) {
- widgets.alert.showAlert(resources.msg_supportCanvas, false);
- return;
- }
- layerItemsContainer = document.getElementsByClassName('layerItems')[0];
- layerItems = document.getElementById('layerItems');
- map = new SuperMap.Map("map", {
- controls: [
- new SuperMap.Control.ScaleLine(),
- new SuperMap.Control.Zoom(),
- new SuperMap.Control.MousePosition(),
- new SuperMap.Control.Navigation({
- dragPanOptions: {
- enableKinetic: true
- }
- })]
- });
- var hightLightCartoCss = document.getElementById("textData").text;
- layer = new SuperMap.Layer.TiledVectorLayer("China", url, {
- cacheEnabled: true,
- returnAttributes: true
- }, {useLocalStorage: true, highLightCartoCss: hightLightCartoCss});
- layer.events.on({"layerInitialized": addLayer});
- map.events.on({
- 'click': function (evt) {
- closeInfoWin();
- layer.unHightlightFeatures();
- featureInfoes = layer.getFeature(evt.xy.x, evt.xy.y);
- console.log(featureInfoes);
- if (featureInfoes && featureInfoes.length > 0) {
- while (layerItems.firstChild) {
- layerItems.removeChild(layerItems.firstChild);
- }
- layerItemsContainer.style.top = evt.clientY + 'px';
- layerItemsContainer.style.left = evt.clientX + 'px';
- for (var i = 0, len = featureInfoes.length; i < len; i++) {
- var li = document.createElement('li');
- li.innerHTML = featureInfoes[i].cartoLayer.layerName;
- console.log(featureInfoes[i]);
- li.setAttribute('data-index', i);
- layerItems.appendChild(li);
- if (i !== (len - 1)) {
- li.style.borderBottom = '1px solid';
- }
- li.onclick = liClickHandle;
- }
- layerItemsContainer.style.display = 'block';
- } else {
- layerItemsContainer.style.display = 'none';
- }
- },
- 'rightclick': function () {
- layerItemsContainer.style.display = 'none';
- },
- 'move': function () {
- layerItemsContainer.style.display = 'none';
- }
- });
- }
- function liClickHandle(evt) {
- var evt = window.event || evt;
- var target = evt.srcElement || evt.target;
- evt.stopPropagation();
- var index = +target.dataset.index;
- layer.highlightFeatures(featureInfoes[index]);
- var lonlat = map.getLonLatFromViewPortPx(featureInfoes.xy);
- openPopup(featureInfoes[index].feature, lonlat);
- layerItemsContainer.style.display = 'none';
- }
- function addLayer() {
- map.addLayers([layer]);
- var center = new SuperMap.LonLat(0, 0);
- map.setCenter(center, 4);
- }
- //定义mouseClickHandler函数,触发click事件会调用此函数
- function openPopup(feature, lonlat) {
- var key = 'NAME';
- var val = feature && feature.attributes && feature.attributes[key];
- if (!val) {
- key = 'SmID';
- val = feature && feature.attributes && feature.attributes[key];
- }
- var contentHTML = "<div style='width:80px; font-size:12px;font-weight:bold ; opacity: 0.8'>";
- contentHTML += key + ":" + val;
- contentHTML += "</div>";
- //初始化FramedCloud类
- framedCloud = new SuperMap.Popup.FramedCloud(
- "chicken",
- lonlat,
- null,
- contentHTML,
- null,
- true,
- null,
- true
- );
- infowin = framedCloud;
- map.addPopup(framedCloud);
- }
- function closeInfoWin() {
- layer.unHightlightFeatures();
- if (infowin) {
- try {
- infowin.hide();
- infowin.destroy();
- }
- catch (e) {
- }
- }
- }
- </script>
- </body>
- </html>
|