在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>
相关推荐
游戏开发爱好者81 小时前
iPhone HTTPS 抓包实战指南,移动端加密流量分析、代理解密失败排查与底层数据流捕获
android·ios·小程序·https·uni-app·iphone·webview
2501_9159090613 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview
lvha15 小时前
uniapp BLE低功耗蓝牙插件 支持安卓 iOS 鸿蒙NEXT 微信小程序
uni-app·蓝牙
Redundantº20 小时前
Uniapp 适配安卓与 iOS 的 PDF、DOC 文件上传
android·ios·pdf·uni-app·webview
Harry技术21 小时前
UniApp H5 代理失效的终极替代方案
uni-app
2501_915918411 天前
iOS 应用如何防止破解?从逆向链路还原攻击者视角,构建完整的反破解工程实践体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_916007471 天前
iOS 压力测试的工程化体系 构建多工具协同的极限稳定性验证方案
android·ios·小程序·uni-app·压力测试·iphone·webview
2501_916007471 天前
iOS 应用上架流程的工程化拆解 从签名体系到提交审核的全过程管控
android·ios·小程序·https·uni-app·iphone·webview
一殊酒1 天前
【前端开发】Uniapp:Android/IOS云打包
android·ios·uni-app
yqcoder1 天前
uni-app 之 uni.showLoading
uni-app