在uniapp中如何使用地图

1,技术选择

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

2,webview使用

使用webview必须按照官方文档,官网地址:https://uniapp.dcloud.net.cn/component/web-view.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一定需要注意目录结构。
在这里插入图片描述

<!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>
// 对应的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>

相关推荐

  1. 如何uniapp使用websocket?

    2024-07-13 10:58:02       16 阅读
  2. 如何 Flutter 实现地理定位和地图功能?

    2024-07-13 10:58:02       33 阅读
  3. 如何uniapp编写云函数

    2024-07-13 10:58:02       49 阅读
  4. uniappvue2版本的uniapp使用npm安装高德地图

    2024-07-13 10:58:02       30 阅读
  5. uviewplusuniapp的配置使用

    2024-07-13 10:58:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 10:58:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 10:58:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 10:58:02       58 阅读
  4. Python语言-面向对象

    2024-07-13 10:58:02       69 阅读

热门阅读

  1. 菜单(Menu)

    2024-07-13 10:58:02       20 阅读
  2. QAbstractButton

    2024-07-13 10:58:02       20 阅读
  3. Fastadmin之 按钮触发弹窗

    2024-07-13 10:58:02       25 阅读
  4. 我会什么开发技能

    2024-07-13 10:58:02       30 阅读
  5. iptables配置网络地址转换(nat)

    2024-07-13 10:58:02       28 阅读
  6. 【STM32 ARM】区分MCU,MPU与AP

    2024-07-13 10:58:02       21 阅读
  7. LeetCode 每日一题 2024/7/8-2024/7/14

    2024-07-13 10:58:02       27 阅读
  8. 工作需求第一次写千行SQL语句

    2024-07-13 10:58:02       21 阅读
  9. 项目管理开发实战

    2024-07-13 10:58:02       29 阅读