### Install localForage Dependencies Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/localforage/README.md Setup the development environment by cloning the repository and installing npm and bower dependencies. ```bash # Install bower globally if you don't have it: npm install -g bower # Replace USERNAME with your GitHub username: git clone git@github.com:USERNAME/localForage.git cd localForage npm install bower install ``` -------------------------------- ### Start Development Server Source: https://github.com/marsgis/mars2d-es5-example/blob/master/README.md Execute this command to launch the local development server, allowing you to view and test the examples in your browser. ```bash npm run serve ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/marsgis/mars2d-es5-example/blob/master/README.md Run this command in your project's root directory to install all necessary npm dependencies before running the development server. ```bash npm install //or use a proxy npm i --registry=http://registry.taobao.org ``` -------------------------------- ### Install localForage with npm Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/localforage/README.md Install localForage using npm for use in your project. ```bash npm install localforage ``` -------------------------------- ### Get Example ID (ES5) Source: https://github.com/marsgis/mars2d-es5-example/blob/master/editor-es5.html Extracts and formats an example ID from the URL's query string. It retrieves the 'id' parameter, cleans it by replacing backslashes with forward slashes, and removes prefixes like 'example/' and file extensions like '/map.js' or '/index.html'. ```javascript /** * 获取示例ID * @export * @return {string} 示例ID */ function getExampleId() { let exampleId = getQueryString("id") if (exampleId) { return exampleId.replace(/\\/gm, "/").replace("example/", "").replace("/map.js", "").replace("/index.html", "") } } ``` -------------------------------- ### Install jQuery MiniColors via NPM Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/README.md Use this command to install the official NPM version of MiniColors. Ensure you use this version for the latest updates. ```bash npm install --save @claviska/jquery-minicolors ``` -------------------------------- ### Clone Mars2D ES5 Example with Gitee Source: https://github.com/marsgis/mars2d-es5-example/blob/master/README.md Use this command to clone the project repository from Gitee, which may offer faster download speeds in China. ```bash git clone git@gitee.com:marsgis/mars2d-es5-example.git ``` -------------------------------- ### Clone Mars2D ES5 Example with Git Source: https://github.com/marsgis/mars2d-es5-example/blob/master/README.md Use this command to clone the project repository from GitHub using Git. ```bash git clone git@github.com:marsgis/mars2d-es5-example.git ``` -------------------------------- ### Get or Set Settings Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Gets or sets the control's settings. Providing new settings will re-initialize the control. ```javascript $(_selector_).minicolors('settings', { control: 'brightness' }); ``` -------------------------------- ### Initialize and Bind Map Events Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/label/index.html Sets up the map and binds event listeners for UI interactions, including drawing, data loading, and layer management. This is the main entry point for the example's functionality. ```javascript "use script" //开发环境建议开启严格模式 var map function initUI() { bindAttributePannel() //新增绘制 $("#btnStartDraw").click(function () { //开始绘制(调用map.js里面的方法) startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { //大数据加载(调用map.js里面的方法) addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效 $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val //启用编辑 }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val //显示隐藏 }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { $message("当前没有标注任何数据,无需保存!") return } const geojson = graphicLayer.toGeoJSON() mars2d.Util.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() if (fileType === "json" || fileType === "geojson") { const reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { const json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) } } else if (fileType === "kml") { const reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { const strkml = this.result mapWork.kgUtil.toGeoJSON(strkml).then((geojson) => { console.log("kml2geojson转换结果为", geojson) graphicLayer.loadGeoJSON(geojson, { flyTo: true }) }) } } else if (fileType === "kmz") { // 加载input文件控件的二进制流 mapWork.kgUtil.toGeoJSON(item).then((geojson) => { console.log("kmz2geojson", geojson) graphicLayer.loadGeoJSON(geojson, { flyTo: true }) }) } else { message.error("暂不支持 " + fileType + " 文件类型的数据!") } }) } // ************************************ 属性面板 ************************************ // 绑定事件,激活属性面板 function bindAttributePannel() { graphicLayer.on(mars2d.EventType.drawCreated, function (e) { let val = $("#hasEdit").is(":checked") if (val) { showEditor(e) } }) // 修改了矢量数据 graphicLayer.on("editStart editMovePoint editStyle editRemovePoint", function (e) { showEditor(e.graphic) }) // 停止编辑 graphicLayer.on("editStop removeGraphic", function (e) { setTimeout(() => { if (!graphicLayer.isEditing) { stopEditing() } }, 100) }) } //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】 let timeTik function showEditor(graphic) { clearTimeout(timeTik) let plotAttr = es5widget.getClass("widgets/plotAttr/widget.js") if (plotAttr && plotAttr.isActivate) { plotAttr.startEditing(graphic, graphic.coordinates) } else { es5widget.activate({ map: map, uri: "widgets/plotAttr/widget.js", name: "属性编辑", graphic: graphic, lonlats: graphic.coordinates }) } } function stopEditing() { timeTik = setTimeout(function () { es5widget.disable("widgets/plotAttr/widget.js") }, 200) } //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】 ``` -------------------------------- ### Install leaflet-wind with pnpm Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/mars2d/thirdParty/wind/README.md Use this command to install the leaflet-wind package as a dependency. ```bash pnpm i leaflet-wind -S ``` -------------------------------- ### Initialize UI and Bind Events Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/polyDecorator/index.html Sets up the user interface by binding event handlers for various graphic layer operations. This includes starting drawing, loading data, and toggling layer properties. ```javascript "use script" //开发环境建议开启严格模式 function initUI() { bindAttributePannel() //新增绘制 $("#btnStartDraw").click(function () { //开始绘制(调用map.js里面的方法) startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { //大数据加载(调用map.js里面的方法) addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val //启用编辑 }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val //显示隐藏 }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) function clearSelectFile() { if (!window.addEventListener) { document.getElementById("input_draw_file").outerHTML += "" //IE } else { document.getElementById("input_draw_file").value = "" //FF } } $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Get Item with Async/Await Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/localforage/README.md Demonstrates retrieving an item using async/await syntax for cleaner asynchronous code. ```javascript try { const value = await localforage.getItem('somekey'); // This code runs once the value has been loaded // from the offline store. console.log(value); } catch (err) { // This code runs if there were any errors. console.log(err); } ``` -------------------------------- ### Initialize UI and Bind Events Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/bouncingMarker/index.html Sets up the user interface by binding various event handlers for map interactions and graphic layer management. This includes starting drawing, loading data, and toggling layer properties. ```javascript "use script" //开发环境建议开启严格模式 function initUI() { bindAttributePannel() //新增绘制 $("#btnStartDraw").click(function () { //开始绘制(调用map.js里面的方法) startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { //大数据加载(调用map.js里面的方法) addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val //启用编辑 }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val //显示隐藏 }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) function clearSelectFile() { if (!window.addEventListener) { document.getElementById("input_draw_file").outerHTML += "" //IE } else { document.getElementById("input_draw_file").value = "" //FF } } $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Initialize UI and Bind Events for Graphic Markers Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/marker/index.html Sets up the user interface and binds event listeners for graphic marker interactions. This includes starting drawing, loading data, and managing popup/tooltip/context menu bindings. ```javascript "use script" function initUI() { bindAttributePannel() //新增绘制 $("#btnStartDraw").click(function () { //开始绘制(调用map.js里面的方法) startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { //大数据加载(调用map.js里面的方法) addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val //启用编辑 }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val //显示隐藏 }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Minicolors Opacity Management Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Methods for getting and setting the opacity of the color picker. ```APIDOC ## opacity ### Description Gets or sets a control's opacity level. To use this method as a setter, pass data in as a value between 0 and 1. You can also obtain this value by checking the input element's `data-opacity` attribute. To set a preset opacity value, populate the `data-opacity` attribute of the original input element. ### Method $(_selector_).minicolors('opacity', data) ### Parameters #### Path Parameters - **data** (number) - Optional - The opacity value between 0 and 1. ``` -------------------------------- ### Query Data from GeoServer Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/query/geoserver/index.html Triggers a data query after clearing the map and getting the input text. Assumes a 'query' function is defined elsewhere to handle the actual WFS request. ```javascript function queryData() { clearAll(true); let queryVal = $.trim($("#queryText").val()); query(queryVal); } ``` -------------------------------- ### Initialize UI and Bind Events Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/divGraphic/basis/index.html Initializes the user interface by binding event handlers for various graphic layer operations. This includes starting drawing, adding random graphics, toggling popup and tooltip bindings, managing editability, and handling file import/export. ```javascript "use script" function initUI() { bindAttributePannel() $("#btnStartDraw").click(function () { startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Initialize UI and Bind Events for Graphic Layer Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/circle/index.html Initializes the user interface by binding event handlers for various graphic layer operations. This includes starting drawing, loading random graphics, toggling popup/tooltip/context menu visibility, enabling editing, clearing graphics, and handling file import/export. ```javascript "use script" //开发环境建议开启严格模式 function initUI() { bindAttributePannel() //新增绘制 $("#btnStartDraw").click(function () { //开始绘制(调用map.js里面的方法) startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { //大数据加载(调用map.js里面的方法) addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics\[graphics.length - 1\] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) //可在图层上绑定tooltip,对所有加到这个图层的矢量数据都生效 $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val //启用编辑 }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val //显示隐藏 }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) $("#input_draw_file").change(function (e) { let file = this.files\[0\] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Initialize UI and Bind Events for Rectangle Graphics Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/rectangle/index.html Initializes the user interface and binds event handlers for various rectangle graphic operations. This includes starting drawing, loading data, and managing display options like popups, tooltips, and editing. ```javascript "use script" function initUI() { bindAttributePannel() $("#btnStartDraw").click(function () { startDrawGraphic() }) $("#addRandomGraphicByCount").click(function () { addRandomGraphicByCount($("#count").val()) graphicLayer.flyTo({ duration: 0, heading: 0, pitch: -40, scale: 1.2 }) const graphics = graphicLayer.getGraphics() editorGraphic = graphics[graphics.length - 1] }) $("#chkPopup").change(function () { let val = $(this).is(":checked") if (val) { bindLayerPopup(graphicLayer) } else { graphicLayer.unbindPopup() } }) $("#chkTooltip").change(function () { let val = $(this).is(":checked") if (val) { graphicLayer.bindTooltip("我是layer上绑定的Tooltip") } else { graphicLayer.unbindTooltip() } }) $("#chkContextMenu").change(function () { let val = $(this).is(":checked") if (val) { bindLayerContextMenu(graphicLayer) } else { graphicLayer.unbindContextMenu(true) } }) $("#chkHasEdit").change(function () { let val = $(this).is(":checked") graphicLayer.hasEdit = val }) $("#chkShow").change(function () { let val = $(this).is(":checked") graphicLayer.show = val }) $("#btnClear").click(function () { graphicLayer.clear() }) $("#btnExpFile").click(function () { if (graphicLayer.length === 0) { window.layer.msg("当前没有标注任何数据,无需保存!") return } let geojson = graphicLayer.toGeoJSON() haoutil.file.downloadFile("我的标注.json", JSON.stringify(geojson)) }) $("#btnImpFile").click(function () { $("#input_draw_file").click() }) function clearSelectFile() { if (!window.addEventListener) { document.getElementById("input_draw_file").outerHTML += "" } else { document.getElementById("input_draw_file").value = "" } } $("#input_draw_file").change(function (e) { let file = this.files[0] let fileName = file.name let fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length).toLowerCase() if (fileType == "json" || fileType == "geojson") { let reader = new FileReader() reader.readAsText(file, "UTF-8") reader.onloadend = function (e) { let json = this.result graphicLayer.loadGeoJSON(json, { flyTo: true }) clearSelectFile() } } else { window.layer.msg("暂不支持 " + fileType + " 文件类型的数据!") clearSelectFile() } }) } ``` -------------------------------- ### Initialize UI and Add Cluster Layers Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/layer-graphic/basis/cluster/index.html Sets up the user interface by attaching click handlers to buttons for adding and clearing layers. It then calls addLayer() to initially display a cluster layer. ```javascript "use strict" //开发环境建议开启严格模式 let clusterLayer function initUI() { //新增绘制 $("#btnAddData").click(function () { addLayer() }) $("#btnAddData2").click(function () { addCustomLayer() }) $("#btnAddData3").click(function () { addCustomPhotoLayer() }) $("#btnClear").click(function () { removeLayer() }) addLayer() } ``` -------------------------------- ### Instantiation Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Demonstrates how to initialize the MiniColors plugin on an input element using jQuery. ```APIDOC ## Instantiation Instantiate like any other jQuery plugin: ```javascript $('INPUT.minicolors').minicolors(settings); ``` ``` -------------------------------- ### Initialize UI and Event Handlers Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/layer-graphic/basis/canvas-marker/index.html Sets up the user interface and attaches click event handlers for clearing data, showing standard markers, and showing custom-drawn markers. Includes performance timing for marker loading. ```javascript "use strict" //开发环境建议开启严格模式 // var map // let canvasMarkerLayer function initUI() { $("#btnClearData").click(function () { // canvasMarkerLayer.clear() clearData() }) $("#btnShowData").click(function () { haoutil.loading.show() let startTime = new Date().getTime() let numPoints = Number($("#txtCount").val()) * 10000 addMarkers(numPoints) haoutil.loading.close() let endTime = new Date().getTime() // 两个时间戳相差的毫秒数 let usedTime = (endTime - startTime) / 1000 console.log(usedTime) haoutil.msg("共耗时" + usedTime.toFixed(2) + "秒") }) $("#btnShowData2").click(function () { haoutil.loading.show() let startTime = new Date().getTime() let numPoints = Number($("#txtCount").val()) * 10000 addCustomDrawMarkers(numPoints) haoutil.loading.close() let endTime = new Date().getTime() // 两个时间戳相差的毫秒数 let usedTime = (endTime - startTime) / 1000 console.log(usedTime) haoutil.msg("共耗时" + usedTime.toFixed(2) + "秒") }) } ``` -------------------------------- ### Initialize and Export Scene Image Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/thing/expImg/index.html Initializes the ExpImg module and sets up event handlers for exporting the scene. Use `expAll` to export the entire scene, with an option to display it as a base64 encoded image without downloading. ```javascript "use strict" //开发环境建议开启严格模式 function initUI(options) { //合并属性参数,可覆盖config.json中的对应配置 let mapOptions = mars2d.Util.merge(options, { center: { lat: 33.588378, lng: 119.031749, alt: 172, heading: 3, pitch: -23 } }) let expImg = new mars2d.thing.ExpImg({ eleid: "centerDiv2D" //导出的div的id }) map.addThing(expImg) $("#showMapImg").click(function () { expImg.expAll({ download: false, calllback: function (base64) { window.layer.open({ type: 1, title: "场景出图", skin: "layer-mars-dialog animation-scale-up", resize: true, area: ["50%", "60%"], //宽高 content: "' }) } }) }) $("#downLoad").click(function () { expImg.expAll() }) $("#downLoad2").click(function () { expImg.expByDraw() }) } ``` -------------------------------- ### Get or Set Value Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Gets or sets the color value of the control. Accepts a color string or an object with 'color' and 'opacity' properties. ```javascript $(_selector_).minicolors('value', '#ff0000'); $(_selector_).minicolors('value', { color: '#00f', opacity: 0.5 }); ``` -------------------------------- ### Initialize UI and Event Handlers Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/query/geoserver/index.html Sets up the user interface, including drawing tools and a data table. It attaches event listeners for map events and UI interactions. ```javascript "use strict"; //开发环境建议开启严格模式 let $table; function initUI(options) { geoJsonLayer.on(mars2d.EventType.load, geoJsonLayer_onLoadHandler); //框选查询 多边 $("#drawPolygon").click(function () { clearAll(); map.graphicLayer.startDraw({ type: "polygon", style: { color: "#00FF00", opacity: 0.3, outline: true, outlineColor: "#20a0ff", clampToGround: true }, success: function (graphic) { drawGraphic = graphic; } }); }); //框选查询 矩形 $("#drawRectangle").click(function () { clearAll(); map.graphicLayer.startDraw({ type: "rectangle", style: { color: "#00FF00", opacity: 0.3, outline: true, outlineColor: "#20a0ff", clampToGround: true }, success: function (graphic) { drawGraphic = graphic; } }); }); //框选查询 圆 $("#drawCircle").click(function () { clearAll(); map.graphicLayer.startDraw({ type: "circle", style: { color: "#00FF00", opacity: 0.3, outline: true, outlineColor: "#20a0ff", clampToGround: true }, success: function (graphic) { drawGraphic = graphic; } }); }); $("#removeAll").click(function () { clearAll(); }); $("#query").click(function () { queryData(); }); $table = $("#table"); $table.bootstrapTable({ height: 300, singleSelect: true, //单选 iconsPrefix: "fa", pagination: true, showPaginationSwitch: false, pageNumber: 1, pageSize: 5, pageList: [5, 10, 20, 50, 100], columns: [ { title: "", //序号 sortable: false, align: "center", width: 50, formatter: function (value, row, index) { return index + 1; } }, { field: "项目名称", title: "名称", sortable: true, width: 100 }, { field: "设施类型", title: "类型", sortable: true, align: "center", width: 70 }, { field: "具体位置", title: "地址", sortable: true, formatter: function (value, row) { if (value) { return value; } else { return ""; } } } ], onClickRow: function (item, $element, field) { let graphic = item.graphic; if (graphic == null) { toastr.warning(item.name + " 无经纬度坐标信息!"); return; } map.flyToGraphic(graphic); } }); } ``` -------------------------------- ### Get or Set Opacity Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Gets or sets the opacity level of the color picker. Pass a value between 0 and 1 to set the opacity. The opacity can also be set via the 'data-opacity' attribute. ```javascript $(_selector_).minicolors('opacity', 0.5); ``` -------------------------------- ### Build Project for Production Source: https://github.com/marsgis/mars2d-es5-example/blob/master/README.md This command compiles and optimizes the project for deployment. The output will be in the 'dist' directory. ```bash npm run build //Compile and generate output in the dist directory for deployment npm run serve:dist //Test the running status of the dist directory ``` -------------------------------- ### Basic MiniColors Instantiation Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Demonstrates the fundamental way to instantiate the MiniColors plugin on an input element with the class 'minicolors'. This is the simplest form of initialization. ```javascript $"INPUT.minicolors").minicolors(settings); ``` -------------------------------- ### Get RGB or RGBA String Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Returns the current color value as an RGB or RGBA string, suitable for CSS. ```javascript var rgbString = $(_selector_).minicolors('rgbString'); var rgbaString = $(_selector_).minicolors('rgbaString'); ``` -------------------------------- ### Initialize UI and Event Listeners Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/map/manager/pointTrans/index.html Sets up the user interface by handling radio button changes for coordinate format selection and updating the display accordingly. It also initializes the map's load event to display the center point. ```javascript "use strict" // Global intermediate variables let currJD let currWD eventTarget.on("loadOK", function (e) { // Default display map center point coordinates let point = e.point point.format() currJD = point.lng currWD = point.lat updateTen() }) function initUI() { $("input:radio[name=\"rdoType\"]").change(function () { let selectType = $(this).val() switch (selectType) { default: // Decimal $(".viewTen").show() $(".viewDms").hide() $(".viewGk").hide() updateTen() break case "2": // Degrees Minutes Seconds $(".viewDms").show() $(".viewTen").hide() $(".viewGk").hide() updataDfm() break case "3": // CGCS2000 $(".viewTen").hide() $(".viewDms").hide() $(".viewGk").show() updata3GKZone() updata6GKZone() break } }) $("input:radio[name=\"rdoGkType\"]").change(function () { let selectType2 = $(this).val() switch (selectType2) { default: $(".viewGk3").show() $(".viewGk6").hide() updata3GKZone() break case "2": // Current view range $(".viewGk3").hide() $(".viewGk6").show() updata6GKZone() break } }) } ``` -------------------------------- ### Get RGB Object Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/jquery/minicolors/index.html Returns an object containing the red, green, blue, and alpha components of the current color value. ```javascript var rgb = $(_selector_).minicolors('rgbObject'); // Example: { r: 0, g: 82, b: 148, a: 0.75 } ``` -------------------------------- ### Get Query String Parameter Source: https://github.com/marsgis/mars2d-es5-example/blob/master/read-es5.html Retrieves the value of a specified query string parameter from the current URL. Returns null if the parameter is not found. ```javascript function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=(\[^&]\*)(&|$)") var r = window.location.search.substr(1).match(reg) if (r != null) { return decodeURI(r[2]) } return null } ``` -------------------------------- ### Draw Point Feature Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/turf/buffer/index.html Clears existing graphics and starts a point drawing mode with specified styles. The drawn point will have a pixel size of 6 and a blue color. ```javascript function drawPoint() { deleteAll() graphicLayer.startDraw({ type: "point", style: { pixelSize: 6, color: "#0000ff" } }) } ``` -------------------------------- ### Initialize UI and Keyboard Event Handling Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/map/manager/keyboardRoam/index.html Initializes a slider for step control and sets up a keydown event listener to manage active key highlighting and potentially map movement. Requires jQuery and a slider component. ```javascript function initUI(map) { $("#step") .slider({ min: 0, max: 300, value: 10 }) .on("change", (e) => { changeSlider(e.value.newValue) }) let activeKey = "" $(document).keydown(function (event) { // 在这里处理键盘事件 $(".zm_${activeKey}").removeClass("active") activeKey = event.key $(".zm_${activeKey}").addClass("active") }) } ``` -------------------------------- ### Activate Attribute Editing Widget Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/ellipse/index.html Manages the activation and deactivation of the attribute editing widget. It either starts editing an existing graphic or activates the widget for a new one. ```javascript //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】 let timeTik function showEditor(graphic) { clearTimeout(timeTik) let plotAttr = es5widget.getClass("widgets/plotAttr/widget.js") if (plotAttr && plotAttr.isActivate) { plotAttr.startEditing(graphic, graphic.coordinates) } else { es5widget.activate({ map: map, uri: "widgets/plotAttr/widget.js", name: "属性编辑", graphic: graphic, lonlats: graphic.coordinates }) } } function stopEditing() { timeTik = setTimeout(function () { es5widget.disable("widgets/plotAttr/widget.js") }, 200) } ``` -------------------------------- ### Activate Property Editing Widget Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/layer-graphic/draw/draw/index.html Manages the activation and deactivation of a property editing widget for graphics. It handles starting the editing process and ensuring the widget is properly managed. ```javascript //附加:激活属性编辑widget【非必需,可以注释该方法内部代码】 let timeTik function startEditing(graphic) { clearTimeout(timeTik) let plotAttr = es5widget.getClass("widgets/plotAttr/widget.js") if (plotAttr && plotAttr.isActivate) { plotAttr.startEditing(graphic, graphic.coordinates) } else { es5widget.activate({ map: map, uri: "widgets/plotAttr/widget.js", name: "属性编辑", graphic: graphic, lonlats: graphic.coordinates }) } } function stopEditing() { timeTik = setTimeout(function () { es5widget.disable("widgets/plotAttr/widget.js") }, 200) } ``` -------------------------------- ### UI Initialization and Tab Switching Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/query/arcgisPolygon/index.html This function initializes the user interface, including setting up click handlers for query and clear buttons. It also implements the logic for switching between different content views (table, pie chart, bar chart) based on user clicks on tab list items. ```javascript //table栏切换Ui function initUI() { $("#query").click(function () { let queryVal = $.trim($("#queryText").val()) queryData(queryVal) }) $("#clearAll").click(function () { $("#tab_check").hide() clearAll() }) let tab_list = document.querySelector(".tab_list") let lis = tab_list.querySelectorAll("li") let item = document.querySelectorAll(".item") for (let i = 0; i < lis.length; i++) { lis[i].setAttribute("index", i) lis[i].onclick = function () { for (let i = 0; i < lis.length; i++) { lis[i].className = "" } this.className = "current" let index = this.getAttribute("index") for (let j = 0; j < item.length; j++) { item[j].style.display = "none" } item[index].style.display = "block" } } } ``` -------------------------------- ### Activate Attribute Editor Widget Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/polyDecorator/index.html Manages the activation and interaction with the attribute editor widget. It either starts editing an existing graphic or activates the widget to create a new one. ```javascript let timeTik function showEditor(graphic) { clearTimeout(timeTik) let plotAttr = es5widget.getClass("widgets/plotAttr/widget.js") if (plotAttr && plotAttr.isActivate) { plotAttr.startEditing(graphic, graphic.coordinates) } else { es5widget.activate({ map: map, uri: "widgets/plotAttr/widget.js", name: "属性编辑", graphic: graphic, lonlats: graphic.coordinates }) } } function stopEditing() { timeTik = setTimeout(function () { es5widget.disable("widgets/plotAttr/widget.js") }, 200) } ``` -------------------------------- ### Initialize Mars2D Map and UI Source: https://github.com/marsgis/mars2d-es5-example/blob/master/example/graphic/basis/image-canvas/index.html Sets up the Mars2D map, fits it to specified bounds, and changes the cursor style for the map container. Ensure the Leaflet library is loaded. ```javascript "use strict"; // 开发环境建议开启严格模式 var map function initUI() { map.fitBounds(L.latLngBounds([0, 70], [60, 140])) $(".leaflet-container").css("cursor", "crosshair") } ``` -------------------------------- ### KML to GeoJSON Conversion (XML Document) Source: https://github.com/marsgis/mars2d-es5-example/blob/master/lib/geojson/README.md Converts KML data from an XML document object to GeoJSON format. This example uses jQuery's AJAX to fetch the KML content. ```javascript //加载kml文档对象 $.ajax('https://data.mars2d.cn/file/kml/dg8.kml').done(function (xml) { kgUtil.toGeoJSON(xml).then((geojoson) => { console.log(geojoson) }) }) ```