uniapp地图基本使用及解决添加markers不生效问题?

uniapp地图使用

App端 通过 nvue 页面实现地图

效果图

在这里插入图片描述


template

<template>
	<view class="mapWrap">
		<!-- #ifdef APP-NVUE -->
		<map class="map-container" id="map" :longitude="location.lng" :latitude="location.lat" 
			:show-location="true" :enable-3D="enable3D" :markers="markers" :include-points="includePoints" @callouttap="oncallouttap">
		</map>
		
		<!-- 定位当前位置 -->
		<cover-view class="myLocation fcc">
			<cover-image class="img" src="@/static/image/icon/currnet_location.png" @click="moveToMyLocation"></cover-image>
		</cover-view>
		<!-- #endif -->
	</view>
</template>

js

添加 marker
使用地图查看位置
移到到当前位置
<script>
    export default {
   
		data() {
   
			return {
   
				location: {
   
					lng: 114.058279,
					lat: 22.505375
				},
				markers: [],
				includePoints: []
			}
		},
		onLoad() {
   
			let tmpLocation = this.$store.state.location;
			if (tmpLocation.lng) {
   
				this.location = tmpLocation;
			}
        },
        onReady() {
   
		    this.map = uni.createMapContext("map", this);
		},
        methods: {
   
            getData() {
   
                // ....
                // 测试数据
                this.dataList = [
					{
   
						name: "长安街",
						location: {
   
							lat: 39.907145,
							lng: 116.396651
						}
					},
					{
   
						name: "天安门",
						location: {
   
							lat: 39.908823,
							lng: 116.39747
						}
					}
				]
                
                // 添加 marker
                this.addMarkers();
            },
            /**
             * 添加marker
             */
            addMarkers() {
   
                let markerList = [];
                this.batteryList.forEach((item, index) => {
   
                    let location = item.location;
                    if (location) {
   
                        // 直接使用 this.markers.push()方式,无法添加 marker
                        markerList.push({
   
                            id: Number(index + 1),
                            latitude: Number(location.lat),
                            longitude: Number(location.lng),
                            title: item.name,
                            zIndex: '1',
                            rotate: 0,
                            width: 30,
                            height: 30,
                            anchor: {
   
                                x: 0.5,
                                y: 1
                            },
                            callout: {
   
                                content: item.name,
                                color: '#fff',
                                fontSize: 10,
                                borderRadius: 4,
                                borderWidth: 1,
                                borderColor: '#fb6620',
                                bgColor: '#f7b733',
                                padding: '5',
                                display: 'ALWAYS'
                            },
                            iconPath: '/static/image/icon/location01.png'
                        })
                    }
                     this.markers = markerList;
                })
            },
            /**
             * 气泡事件
             */
            oncallouttap(e) {
   
				const {
    markerId } = e.detail;
				const marker = this.markers.find((item) => item.id === markerId);
                // 使用地图查看位置
				uni.openLocation({
   
					latitude: marker.latitude,
					longitude: marker.longitude,
					name: marker.title,
					success: (res) => {
   
						console.log('success');
					}
				});
			},
            /**
             * 移到到当前位置
             */
			moveToMyLocation() {
   
                let latitude = this.location.lat;
                let longitude = this.location.lng;
				this.map.moveToLocation({
   
					latitude,
					longitude,
					success: (res) => {
   
						this.includePoints = [{
   
							latitude,
							longitude
						}];
					}
				});
			}
        }
	}
</script>

最近更新

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

    2023-12-05 18:40:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-05 18:40:06       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-05 18:40:06       82 阅读
  4. Python语言-面向对象

    2023-12-05 18:40:06       91 阅读

热门阅读

  1. Vue 打包上线后的缓存问题

    2023-12-05 18:40:06       64 阅读
  2. AJAX的基础知识点

    2023-12-05 18:40:06       65 阅读
  3. 03-微服务架构构建之微服务拆分

    2023-12-05 18:40:06       63 阅读
  4. 23种设计模式【C#代码举例】

    2023-12-05 18:40:06       50 阅读
  5. 利用jQuery实现AJAX定时刷新局部页面实例

    2023-12-05 18:40:06       72 阅读
  6. 蓝桥杯ACwing习题

    2023-12-05 18:40:06       51 阅读
  7. Django回顾4

    2023-12-05 18:40:06       37 阅读
  8. Django 用户验证与权限管理

    2023-12-05 18:40:06       64 阅读
  9. C语言K&R圣经笔记 4.1函数基础

    2023-12-05 18:40:06       59 阅读
  10. Linux网络编程 SQLite库(TCP Socket 服务器 客户端)

    2023-12-05 18:40:06       53 阅读
  11. docker 推送tar包到远程仓库

    2023-12-05 18:40:06       62 阅读
  12. 开源C++智能语音识别库whisper.cpp开发使用入门

    2023-12-05 18:40:06       58 阅读