企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
# 简介 > 本章节主要介绍如何在移动端引入图形系统。以 **农田管护** 移动端、**公共平台** 微信端为示例 # 项目引入gis模块 ## 1. 导入gis模块代码 在项目components内checkout(若无法checkout,可在components内创建同名文件夹再checkout)以下地址 > gis-mobile-base地址:https://www.ztgis.com:2443/svn/java%E9%A1%B9%E7%9B%AE%E4%BB%A3%E7%A0%81_%E6%A1%86%E6%9E%B6/gis-boot-front-core/mobile-components/gis-mobile-base > gis-mobile-map地址:https://www.ztgis.com:2443/svn/java%E9%A1%B9%E7%9B%AE%E4%BB%A3%E7%A0%81_%E6%A1%86%E6%9E%B6/gis-boot-front-core/mobile-components/gis-mobile-map 程序目录结构示例: ![](https://img.kancloud.cn/bf/22/bf22a6c21f4a55f9a9f3262b7df8296a_433x673.png) ## 2. 项目配置 > **全局常量** 文件 ``` // arcgis api路径,app请配置成全路径 export const arcgisApiUrl = '/4.21'; // 代理路径,app端 无固定地址,需要配置 export const arcgisProxyUrlPrefix = [] export const arcgisProxyUrl = '' // arcgis 图片资源,app请配置成全路径 export const arcgisImgUrl = "/assets/img" // 系统编码 export const sysCode = '系统编码' // 坐标系编码 export const srCode = "坐标系编码" ``` > **mian.js** 文件 ``` // 引入全局配置 import { arcgisApiUrl, arcgisProxyUrl, arcgisProxyUrlPrefix, arcgisImgUrl } from "@/common/config/index.js" // 往uni中注入全局配置 uni.$sysConfig = { arcgisApiUrl, arcgisProxyUrl, arcgisProxyUrlPrefix, arcgisImgUrl } // 引入lodash, gis中有使用lodash import lodash from 'lodash' Vue.use(lodash) // 封装请求 // 增加固定sysCode,srCode标识 http.interceptor.gis.js此文件可在gis-mobile-map/api内寻找 import httpInterceptor from "@/utils/request/http.interceptor.gis.js"; import httpApi from "@/utils/request/http.api.js"; Vue.use(httpInterceptor.install, app) Vue.use(httpApi.install, app) ``` > **store** 引入gis组件状态管理 ``` //FIXME:gis import map from '@/components/gis-mobile-map/store/map' Vue.use(Vuex) const store = new Vuex.Store({ state: {}, mutations: {}, actions: {}, modules: { map } }) export default store ``` > **request** 引入需要使用的gis组件接口 ``` // 专题图相关接口 import tmap from "@/components/gis-mobile-map/api/tmap.js"; // 底图 import baseLayer from "@/components/gis-mobile-map/api/base-layer.js"; // gis系统配置相关接口 import mapConfig from "@/components/gis-mobile-map/api/config.js"; // gis查询相关 import mapQuery from "@/components/gis-mobile-map/api/query.js"; //标绘 import marker from "@/components/gis-mobile-map/api/marker" //文件 import gisFile from "@/components/gis-mobile-map/api/file" export default { tmap, baseLayer, mapConfig, mapQuery, marker, gisFile } ``` # 代码使用 ## 1. renderjs模式 => app端 > app端为降低逻辑层和视图层的通信损耗,提高视图交互能力,不支持动态引用类库 > 需要自己写通信及视图 > 地址:https://uniapp.dcloud.io/frame?id=renderjs 示例: > 视图页面及调用页面 ``` <template> <map-renderjs ref="GIS" class="jz-map-renderjs" v-on="listeners"></map-renderjs> </template> <script> import mapRenderjs from './map-renderjs.vue' export default { name: 'jz-map', components: { mapRenderjs }, data () { return { listeners: { mapInit: () => { this.$emit("mapInit"); }, } }, methods: { /**       * 地图初始化函数 * @param {Object} defaultBaseLayer 默认底图 * @param {Object} baseLayers 底图 * @param {Object} baseZj 注记      */ mapInit (defaultBaseLayer, baseLayers, baseZj) { this.$refs.GIS.handleMsg({ action: 'gisInit', param: { extent: this.mapConfig.mapExtent, wkid: this.mapConfig.wkid, wkt: this.mapConfig.wkt, isLoadCountry: this.mapConfig.isLoadCountry, widgets: this.mapConfig.widgets, defaultBaseLayer, baseLayers, baseZj } }) }, // 获取底图 getBaseLayer () { // 获取底图 this.$u.api.baseLayer.getAll().then((res) => { // 将底图数据转化为前端格式 const base = res.result .map((item, i) => { return { ...item, id: 'xm_' + item.layerType + i, opacity: Number(item.opacity), checked: false, group: 'xm', token: item.tokenValue, visible: item.layerType === MAP_PARAM.defaultMapType, } }) .sort((a, b) => { // 进行排序,顺序为矢量、规划、影像 const diff = this.mapTypeSort[a.layerType] - this.mapTypeSort[b.layerType] if (diff == 0) { // 若类型相同,按照是否注记排序 return new Number(a.isTagLayer) - new Number(b.isTagLayer) } return diff }) // 基础底图 const baseLayers \= base.filter((v) \=> { return v.isTagLayer != '1' }) // 注记图层 const baseZj \= base.filter((v) \=> { return v.isTagLayer \== '1' }) // 默认加载 const defaultBaseLayer \= base.filter((v) \=> { return v.isDefault \== '1' }) // 设置全局地图信息 this.$store.commit('setBaseLayer', base) this.mapInit(defaultBaseLayer, baseLayers, baseZj) }) }, }, created () { }, mounted () { this.getBaseLayer() }, } </script> ``` > renderjs页面 ``` <template> <div class="jz-map-renderjs" :change:msg="jzGis.gisHandle" :msg="msg"></div> </template> <script> export default { name: 'jz-map', data() { return { msg: {}, } }, props: {}, methods: { //向renderjs发送消息 handleMsg(msg) { msg.config = uni.$sysConfig this.msg = msg }, // 分发地图事件 gisEmitAction(args) { this.$emit(...args) }, }, mounted() {}, created() {}, } </script> <!--   1、与正文视图只能传递普通json,含方法对象传递时,方法会丢失;   2、无法使用mixins --> <script module="jzGis" lang="renderjs"> import GIS from "@/components/gis-mobile-map/GIS.js"; import MAP_PARAM from '@/components/gis-mobile-map/params/views/map-param' import GIS_ICON_PARAM from "@/components/gis-mobile-map/params/gis/gis-icon-param" import drawLineRenderjs from '@/components/gis-mobile-map/mixins/renderjs/draw-line' import mapSwitchRenderjs from '@/components/gis-mobile-map/mixins/renderjs/map-switch' export default { name: "jz-map-renderjs", data () { return { gis: null, config:{}, mapSwitchUtils:null, drawLineUtils:null, directFuncMap: { //初始化加载 gisInit: this.handleGisInit }, }; }, computed:{ widgetKey() { return MAP_PARAM.widgetKey }, }, methods: { //通信,接收处理消息 gisHandle ({ action, param, config }) { if (!action) { console.warn("通知消息缺少action,无效") return false; } try { this.config = config; this.directFuncMap[action](param, config); } catch (e) { console.error(e) } }, // 初始化加载 // 初始化 handleGisInit (param, config) { // 初始化渲染 GIS.initArcGisJsApi(config.arcgisApiUrl).then(() => { // 地图代理(如果需要的话) if (config?.arcgisProxyUrlPrefix?.length > 0) { config?.arcgisProxyUrlPrefix?.map(urlPrefix => { this.gis.withProxyRule({ urlPrefix: urlPrefix, proxyUrl: config.arcgisProxyUrl }); }); } this.gis.initMapView({ container: this.$el, tileInfo: true, extent: param.extent, wkid: param.wkid || 4490, wkt: param.wkt, // 国家底图DOM country: param.isLoadCountry !== "0" ? MAP_PARAM.defaultMapType : 0, widgets: param.widgets ?? { Zoom: true }, layerList: param.defaultBaseLayer }).then((me) => { //向上请求函数,传递的数据内函数会丢失 this.$ownerInstance.callMethod("gisEmitAction", ['mapInit']) // 加载所有底图 this.loadBaseLayers(param.baseLayers, param.baseZj); }); }); }, // 加载所有底图 loadBaseLayers (baseLayers, baseZj) { if (!baseLayers) { return } // 底图 let list = [...baseLayers, ...baseZj].map(v => { return { ...v, group: `xm`, priority: 2, visible: MAP_PARAM.defaultMapType === v.layerType } }) this.gis.addMany(list); this.mapSwitchUtils = new mapSwitchRenderjs(this.gis,list) }, }, mounted () { this.gis = new GIS()}, created () {} }; ``` ## 2. mixins模式 => 微信端 ``` <template> <div class="map-view"> <map-view-div :mapConfig="mapConfig" @callbackGis="getGis"></map-view-div> </div> </template> <script> import mapViewDiv from '@/components/gis-mobile-map/views/map/index' import { mapMixin } from '@/components/gis-mobile-map/mixins/map' export default { name: "map-view", components: { mapViewDiv }, mixins: [mapMixin], data () { return { }; }, watch: {}, methods: { getGis (g) { if (!this.gis) { this.$emit('update:gis', g); // window.gis = g; g.on('init', () => { this.$emit('gisInit', true) }) } } }, created () { }, mounted () { }, }; </script> ```