在uniapp中如何使用地图

1,技术选择

最好是使用webview + html形式加载,避免打包app时的地图加载问题

2,webview使用

使用webview必须按照官方文档,官网地址:https://uniapp.dcloud.net.cn/component/web-view.html

html 复制代码
<template>
	<view>
	<!--src是html的地址,若需传值,使用以下方式-->
		<web-view class="webview" id="webview" :src="urlc"></web-view>
	</view>
</template>

<script>
	export default {
		onLoad(options) {
		//从其他组件传来的值
		    this.lat = options.lat
		    this.longt = options.longt
		},
		data() { 
			return {
				lat:0,
				longt:0
			}
		},
		computed: {
			urlc:function() {
				return '/hybrid/html/index.html?lat='+this.lat+"&longt="+this.longt
			}
		},
	}
</script>
<style>
</style>

3.对应的html

对应的html一定需要注意目录结构。

html 复制代码
<!DOCTYPE html>
<html>
<head>
  <title>位置</title>
  <!--leaflet开发交互地图-->
  <link rel="stylesheet" href="../leaflet/leaflet.css" />
  <script src="../leaflet/leaflet.js"></script>
</head>
<body>
  <div id="map" style="width: 100%;height: 100%"></div>
</body>
javascript 复制代码
// 对应的js
<script>
	var map = L.map('map').setView([坐标], 13);
//若其他组件给当前html文件传值,比如:坐标信息等
//取url中的参数值
	function getQuery(name) {
		// 正则:[找寻'&' + 'url参数名字' = '值' + '&']('&'可以不存在)
		let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
		let r = window.location.search.substr(1).match(reg);
		if(r != null) {
		    // 对参数值进行解码并取出对应的值
	        return decodeURIComponent(r[2]);
		}
		return null;
	}
	var lat = getQuery('lat')
	var longt = getQuery('longt')
	//地图定位
	var map = L.map('map').setView([lat,longt], 16);
	L.tileLayer('http://t0.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=key值', {
	  						zoomControl: true,
	  						attributionControl: false
	}).addTo(map)
	L.tileLayer('http://t0.tianditu.com/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=key值', {
	  						zoomControl: true,
	  						attributionControl: false
	}).addTo(map)
//添加标记点
	var marker = L.marker([lat,longt])
	map.addLayer(marker) 
  </script>
相关推荐
走,带你去玩35 分钟前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
菌菇汤3 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
某公司摸鱼前端10 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
遗憾随她而去.13 小时前
uniapp 中使用路由导航守卫,进行登录鉴权
前端·uni-app
牧杉-惊蛰17 小时前
uniapp微信小程序css中background-image失效问题
css·微信小程序·uni-app
Little_Code1 天前
uniapp 使用ffmpeg播放rtsp
ffmpeg·uni-app·rtsp
儿歌八万首1 天前
uniapp 和原生插件交互
uni-app·交互
儿歌八万首1 天前
UniApp 中实现智能吸顶 Tab 标签导航效果
前端·javascript·uni-app
前端 贾公子1 天前
在移动端使用 Tailwind CSS (uniapp)
前端·uni-app
源码_V_saaskw10 天前
宇鹿家政服务系统小程序ThinkPHP+UniApp
微信小程序·小程序·uni-app·微信公众平台