uniapp引入arcgis for js

uniapp引入arcgis for js

​ 在前段时间,我刚开始接触uniapp,并接到了在uniapp项目中使用arcgis for js的任务。搜遍了所有的经验帖子也没有一个具体的方法步骤,只有几位大佬分享了可行的结果截图。所以今天写一个小白教程,如有错误,欢迎大家指出。

​ 首先在一个空项目中,引入esri-loader。

npm install--save esri-loader

​ 成功后,在页面中通过renderjs引入esri-loader,注意地图部分需要给出具体高度。

<template>
	<view >
		<view id="myMapView" style=" height: 623px " />
	</view>
</template>

<script module="myMapViews" lang="renderjs">
    //renderjs部分
	import {
		loadModules
	} from 'esri-loader'
	export default {
		name: 'myMapView',
		data() {
			return {};
		},
		mounted() {
			this.createMapView()
		},
		methods: {
			createMapView() {
				const options = {
					url: 'https://js.arcgis.com/4.14/init.js',
					css: 'https://js.arcgis.com/4.14/esri/themes/light/main.css'
				};
				loadModules([
					"esri/Map",
					"esri/views/MapView"
				], options).then(([Map, MapView]) => {
					var map = new Map({
						basemap: "topo-vector"
					});
					var view = new MapView({
						container: "myMapView",
						map: map,
						center: [-118.80500, 34.02700], // longitude, latitude
						zoom: 13
					});
				})
			}
		}
	}
</script>


<scrip>
// service 层
</scrip>




<style>
</style>

效果图如下:

在这里插入图片描述

​ 同时,在renderjs的script中,是无法获取到service层的数据的,也无法使用uni的相关接口(注意:H5是可以正常使用的,但是APP端就会报错,比如uni.request等),所以要通过一定的方式从service层去传输。

​ 如果要进行网络请求目前有两种思路:

​ 1.自己写一个原生ajax请求,在renderjs部分使用。

​ 2.在service层给后台发请求后,将数据传回renderjs部分

    下面简单给出个renderjs和service层通讯例子:
<template>
	<view>
		<!-- service层mapdata改变就会调用renderjs部分的mapDataChanged方法 -->
		<view id="myMapView" :change:mapData="myMapViews.mapDataChanged" :mapData="mapData" style=" height: 500px " />
		<view>
			<uni-button @click="changeMapData">service点击</uni-button>
		</view>
	</view>

</template>

<script module="myMapViews" lang="renderjs">
	//renderjs部分
	import {
		loadModules
	} from 'esri-loader'
	export default {
		name: 'myMapView',
		data() {
			return {};
		},
		mounted() {
			this.createMapView()
		},
		methods: {
			createMapView() {
				const options = {
					url: 'https://js.arcgis.com/4.14/init.js',
					css: 'https://js.arcgis.com/4.14/esri/themes/light/main.css'
				};
				loadModules([
					"esri/Map",
					"esri/views/MapView"
				], options).then(([Map, MapView]) => {
					var map = new Map({
						basemap: "topo-vector"
					});
					var view = new MapView({
						container: "myMapView",
						map: map,
						center: [-118.80500, 34.02700], // longitude, latitude
						zoom: 13
					});
				})
			},
			mapDataChanged(newValue, oldValue, ownerVm, vm) {
				//mapData改变触发
				console.log("newValue:", newValue)
				console.log("oldValue:", oldValue)
				//调用service层的getFromRenderJs方法,并传值
				ownerVm.callMethod('getFromRenderJs', {
					type: 'polyline'
				})
				//这种方法也可以主动调用service层方法
				/*UniViewJSBridge.publishHandler('onWxsInvokeCallMethod', {
					cid: this._$id,
					method: 'loadingClose',
					args: {


					}
				})*/
			}
	
		}
	}

</script>

<script>
	//service层
	export default {
		data() {
			return {
				mapData: {}
			}
		},
		mounted() {},
		methods: {
			changeMapData() {
				//click事件触发的service层方法
				this.mapData = {
					type: 'point'
				}
			},
			getFromRenderJs(params) {
				//renderjs调用的service层方法
				console.log('getFromRenderJs:', params)
			}


		},
	
	}

</script>

<style>
</style>

结果如下:

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
要在UniApp项目中通过npm引入ArcGIS API,可以按照以下步骤进行操作: 1. 确保你的UniApp项目已经初始化并且具有npm的支持。如果还没有,可以在命令行中进入项目目录,执行以下命令进行初始化: ``` uni init ``` 2. 在命令行中,进入你的UniApp项目目录,执行以下命令来安装ArcGIS API的npm包: ``` npm install @arcgis/core ``` 3. 在你的UniApp项目中,找到 `main.js` 文件,这是整个应用的入口文件。 4. 在 `main.js` 文件中,添加以下代码来全局引入ArcGIS API: ```javascript import { loadModules } from '@esri/react-arcgis'; // 在Vue的全局配置中加载ArcGIS API模块 Vue.prototype.$loadModules = loadModules; ``` 5. 现在你可以在你的UniApp项目中的任何页面或组件中使用ArcGIS API了。例如,在一个页面的 `onLoad` 生命周期中加载地图: ```javascript export default { data() { return { map: null }; }, onLoad() { this.$loadModules(['esri/Map', 'esri/views/MapView']).then(([Map, MapView]) => { this.map = new Map({ basemap: 'streets' }); const view = new MapView({ container: this.$refs.mapContainer, map: this.map }); }); } }; ``` 6. 在你的页面模板中,添加一个容器元素来显示地图。例如,在页面的 `template` 中添加以下代码: ```html <view ref="mapContainer" style="width: 100%; height: 100vh;"></view> ``` 7. 运行你的UniApp项目,即可看到引入ArcGIS API在页面中显示地图。 请注意,具体的代码可能会因为你使用ArcGIS API版本而略有不同。此外,还需要确保你的UniApp项目的配置和依赖正确设置,并且ArcGIS API的npm包已经成功安装。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值