在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>
相关推荐
2501_915918416 小时前
iOS 开发全流程实战 基于 uni-app 的 iOS 应用开发、打包、测试与上架流程详解
android·ios·小程序·https·uni-app·iphone·webview
黑马源码库miui520867 小时前
JAVA同城打车小程序APP打车顺风车滴滴车跑腿源码微信小程序打车源码
java·微信·微信小程序·小程序·uni-app
清风细雨_林木木12 小时前
uni-app 和 uni-app x 的区别
uni-app
iOS阿玮12 小时前
期待iOS开发者加入,共同抵制“苹果税”反垄断招募令!
uni-app·app·apple
普通网友12 小时前
支持二次开发的代练App源码:订单管理、代练监控、安全护航功能齐全,一站式解决代练护航平台源码(PHP+ Uni-app)
安全·uni-app·php
蜕变菜鸟12 小时前
uview使用u-popup组件当开启遮罩层禁止下层页面滚动。uniapp弹框禁止页面上下滚动。
uni-app
吴传逞2 天前
记一次uniapp微信小程序开发scss变量失效的问题
微信小程序·uni-app·scss
2501_915921432 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
!win !2 天前
uni-app项目Tabbar实现切换icon动效
小程序·uni-app