Skip to content

地图组件

地图组件是 uni-app 提供的用于展示地图的组件,支持各种地图操作和自定义功能。

map 地图

map 组件用于展示地图,支持标记点、标记线、覆盖物等。

属性说明

属性名类型默认值说明
longitudeNumber中心经度,必填
latitudeNumber中心纬度,必填
scaleNumber16缩放级别,取值范围为 5-18
markersArray标记点
polylineArray路线
circlesArray
polygonsArray多边形
include-pointsArray缩放视野以包含所有给定的坐标点
show-locationBooleanfalse显示带有方向的当前定位点
enable-3DBooleanfalse展示3D楼块
show-compassBooleanfalse显示指南针
enable-overlookingBooleanfalse开启俯视
enable-zoomBooleantrue是否支持缩放
enable-scrollBooleantrue是否支持拖动
enable-rotateBooleanfalse是否支持旋转
enable-satelliteBooleanfalse是否开启卫星图
enable-trafficBooleanfalse是否开启实时路况
enable-poiBooleantrue是否展示 POI 点
enable-buildingBooleantrue是否展示建筑物

markers 属性说明

属性说明类型必填默认值
id标记点idNumber
latitude纬度Number
longitude经度Number
title标注点名String
iconPath显示的图标String
rotate旋转角度Number0
alpha标注的透明度Number1
width标注图标宽度Number图片实际宽度
height标注图标高度Number图片实际高度
callout自定义标记点上方的气泡窗口Object
label为标记点旁边增加标签Object
anchor经纬度在标注图标的锚点,默认底边中点Object

事件说明

事件名说明返回值
@markertap点击标记点时触发event.detail =
@callouttap点击标记点对应的气泡时触发event.detail =
@controltap点击控件时触发event.detail =
@regionchange视野发生变化时触发event.type = begin/end, event.detail =
@tap点击地图时触发event.detail =
@updated地图渲染更新完成时触发

示例代码

vue
<template>
  <view class="container">
    <map
      id="myMap"
      :longitude="longitude"
      :latitude="latitude"
      :scale="scale"
      :markers="markers"
      :polyline="polyline"
      :circles="circles"
      :polygons="polygons"
      :include-points="includePoints"
      :show-location="true"
      :enable-3D="false"
      :show-compass="true"
      :enable-zoom="true"
      :enable-scroll="true"
      @markertap="onMarkerTap"
      @callouttap="onCalloutTap"
      @regionchange="onRegionChange"
      @tap="onMapTap"
      style="width: 100%; height: 300px;"
    ></map>
    
    <view class="map-controls">
      <button type="primary" @click="moveToLocation">移动到当前位置</button>
      <button type="primary" @click="addMarker">添加标记点</button>
      <button type="primary" @click="drawRoute">绘制路线</button>
      <button type="primary" @click="includeAllPoints">显示所有点</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 地图中心点
      longitude: 116.397390,
      latitude: 39.908860,
      scale: 14,
      
      // 标记点
      markers: [{
        id: 1,
        latitude: 39.908860,
        longitude: 116.397390,
        title: '天安门',
        iconPath: '/static/images/marker.png',
        width: 30,
        height: 30,
        callout: {
          content: '天安门广场',
          color: '#000000',
          fontSize: 14,
          borderRadius: 4,
          borderWidth: 1,
          borderColor: '#cccccc',
          bgColor: '#ffffff',
          padding: 5,
          display: 'BYCLICK',
          textAlign: 'center'
        },
        label: {
          content: '天安门',
          color: '#000000',
          fontSize: 12,
          anchorX: 0,
          anchorY: -60,
          borderWidth: 1,
          borderColor: '#ffffff',
          borderRadius: 4,
          bgColor: '#ffffff',
          padding: 2,
          textAlign: 'center'
        }
      }],
      
      // 路线
      polyline: [],
      
      // 圆形
      circles: [{
        latitude: 39.908860,
        longitude: 116.397390,
        color: '#FF0000AA',
        fillColor: '#7cb5ec88',
        radius: 500,
        strokeWidth: 2
      }],
      
      // 多边形
      polygons: [{
        points: [
          {
            latitude: 39.913860,
            longitude: 116.407390
          },
          {
            latitude: 39.913860,
            longitude: 116.387390
          },
          {
            latitude: 39.903860,
            longitude: 116.387390
          },
          {
            latitude: 39.903860,
            longitude: 116.407390
          }
        ],
        strokeWidth: 2,
        strokeColor: '#FF0000AA',
        fillColor: '#7cb5ec88'
      }],
      
      // 包含所有点
      includePoints: [{
        latitude: 39.908860,
        longitude: 116.397390
      }, {
        latitude: 39.913860,
        longitude: 116.407390
      }],
      
      // 地图上下文
      mapContext: null
    }
  },
  onReady() {
    // 获取地图上下文
    this.mapContext = uni.createMapContext('myMap', this);
  },
  methods: {
    // 标记点点击事件
    onMarkerTap(e) {
      console.log('点击了标记点:', e.detail.markerId);
      uni.showToast({
        title: `点击了标记点 ${e.detail.markerId}`,
        icon: 'none'
      });
    },
    
    // 气泡点击事件
    onCalloutTap(e) {
      console.log('点击了气泡:', e.detail.markerId);
      uni.showToast({
        title: `点击了气泡 ${e.detail.markerId}`,
        icon: 'none'
      });
    },
    
    // 地图区域变化事件
    onRegionChange(e) {
      if (e.type === 'end') {
        console.log('地图区域变化:', e.detail);
      }
    },
    
    // 地图点击事件
    onMapTap(e) {
      console.log('点击了地图:', e.detail);
    },
    
    // 移动到当前位置
    moveToLocation() {
      this.mapContext.moveToLocation();
      uni.showToast({
        title: '已移动到当前位置',
        icon: 'none'
      });
    },
    
    // 添加标记点
    addMarker() {
      const newMarker = {
        id: this.markers.length + 1,
        latitude: this.latitude + (Math.random() * 0.01 - 0.005),
        longitude: this.longitude + (Math.random() * 0.01 - 0.005),
        title: `标记点 ${this.markers.length + 1}`,
        iconPath: '/static/images/marker.png',
        width: 30,
        height: 30
      };
      
      this.markers.push(newMarker);
      
      uni.showToast({
        title: `已添加标记点 ${newMarker.id}`,
        icon: 'none'
      });
    },
    
    // 绘制路线
    drawRoute() {
      // 模拟路线点
      const points = [];
      for (let i = 0; i < 5; i++) {
        points.push({
          latitude: this.latitude + (Math.random() * 0.01 - 0.005),
          longitude: this.longitude + (Math.random() * 0.01 - 0.005)
        });
      }
      
      this.polyline = [{
        points: points,
        color: '#5470c6',
        width: 4,
        dottedLine: false,
        arrowLine: true,
        arrowIconPath: '/static/images/arrow.png'
      }];
      
      uni.showToast({
        title: '已绘制路线',
        icon: 'none'
      });
    },
    
    // 显示所有点
    includeAllPoints() {
      const points = [];
      
      // 添加所有标记点
      this.markers.forEach(marker => {
        points.push({
          latitude: marker.latitude,
          longitude: marker.longitude
        });
      });
      
      // 添加当前位置
      points.push({
        latitude: this.latitude,
        longitude: this.longitude
      });
      
      this.includePoints = points;
      
      // 使用地图上下文调整视野
      this.mapContext.includePoints({
        points: points,
        padding: [50, 50, 50, 50]
      });
      
      uni.showToast({
        title: '已显示所有点',
        icon: 'none'
      });
    }
  }
}
</script>

<style>
.container {
  padding: 15px;
}
.map-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-top: 15px;
}
.map-controls button {
  margin-bottom: 10px;
  width: 48%;
}
</style>

注意事项

  1. 在使用地图组件时,需要注意各平台的差异性,某些功能可能在特定平台上不可用。

  2. 使用地图组件需要申请对应平台的地图服务密钥,并进行相应的配置:

    • 微信小程序:需要在微信公众平台配置地图服务 API
    • App:需要在 manifest.json 中配置高德地图或百度地图的 AppKey
    • H5:需要在 manifest.json 中配置腾讯地图的 Key
  3. 地图组件的性能消耗较大,建议合理使用,避免在一个页面中同时显示多个地图组件。

  4. 在进行地图相关操作时,建议使用 mapContext 上下文对象,它提供了更多的控制方法。

  5. 地图标记点的图标路径支持本地路径和网络路径,但建议使用本地路径以提高加载速度。

一次开发,多端部署 - 让跨平台开发更简单