Map
Map 地图【H5】
地图组件(仅适用于H5,使用leaflet)。组件属性尽量对齐 Taro 的 Map 组件
与 Taro 的 Map 组件的差异点:
- marker 的 iconPath 只支持传网络图片或者通过 import 导入的图片,无法直接传本地路径
- marker 的 anchor 属性对应 anchorX、anchorY,且仅当有传入 width、height 时有效
- polyline 的 color 不支持 alpha 通道,要设置透明度请使用 opacity 属性
- 由于高德坐标系和 leaflet 使用的坐标系不同,在传 showLocation 为 true 时定位到的位置在高德地图上会有偏差。建议使用定位 API 后通过传经纬度属性来聚焦中心点
使用组件前请先安装需要的依赖:
pnpm add leaflet leaflet.chinatmsproviders
代码示例
import icon from '@/assets/morning-meeting.png';import { BfMap } from '@bud-fe/react-taro-ui';import { Button, View } from '@tarojs/components';import React, { useState } from 'react';export default () => {const [latLng, setLatLng] = useState<{ lat: number; lng: number }>({ lat: 31.232454, lng: 121.476607 });return (<View><BfMap// showLocationstyle={{ height: 500 }}latitude={latLng.lat}longitude={latLng.lng}scale={16}markers={[{latitude: 31.232454,longitude: 121.476607,id: '来福士',title: '来福士',width: 40,height: 40,iconPath: icon,anchor: { x: 0.5, y: 0.5 },},{ latitude: 31.228234, longitude: 121.475497, id: '上海博物馆', title: '上海博物馆' },]}polyline={[{points: [{ latitude: 31.235099, longitude: 121.473697 },{ latitude: 31.234304, longitude: 121.475752 },{ latitude: 31.232135, longitude: 121.473115 },{ latitude: 31.232432, longitude: 121.476671 },],color: '#B93633',width: 4,opacity: 0.8,},]}onMarkerTap={(e) => {console.log('onMarkerTap', e);}}onCalloutTap={(e) => {console.log('onCalloutTap', e);}}onRegionChange={(e) => {console.log('onRegionChange', e);}}/><ButtononClick={() => {setLatLng({ lat: 31.232454, lng: 121.476607 });}}>move to 来福士广场</Button><ButtononClick={() => {setLatLng({ lat: 31.228234, lng: 121.475497 });}}>move to 上海博物馆</Button></View>);};
API
IBfMapProps
Name | Description | Type | Default |
---|---|---|---|
longitude | 中心经度 | number | -- |
latitude | 中心纬度 | number | -- |
scale | 缩放级别 | number | 16 |
minScale | 最小缩放级别 | number | -- |
maxScale | 最大缩放级别 max:18 | number | -- |
showLocation | 显示当前定位点 | boolean | false |
markers | 标记点 | IBfMapMarker[] | -- |
polyline | 路线 | IBfMapPolyline[] | -- |
onMarkerTap | 点击标记点时触发,e.detail = {markerId} | IBfMapCommonEventFunction<onMarkerTapEventDetail, {}> | -- |
onCalloutTap | 点击标记点对应的气泡时触发,e.detail = {markerId} | IBfMapCommonEventFunction<onCalloutTapEventDetail, {}> | -- |
onRegionChange | 视野发生变化时触发 | IBfMapCommonEventFunction<TOnRegionChangeDetailType, { mpEvent: TOnRegionChangeDetailType; }> | -- |
IBfMapMarker
Name | Description | Type | Default |
---|---|---|---|
id | 标记点id | string | number | -- |
latitude | 纬度 | number | (required) |
longitude | 经度 | number | (required) |
title | 标注点名 @remarks 点击时显示,callout 存在时将被忽略 | string | -- |
zIndex | 显示层级 | number | -- |
iconPath | 显示的图标的图片路径 | string | -- |
width | 标注图标宽度 | number | -- |
height | 标注图标高度 | number | -- |
anchor | 经纬度在标注图标的锚点,默认底边中点 @remarks {x, y},x表示横向(0-1),y表示竖向(0-1)。{x: .5, y: 1} 表示底边中点 | { x: number; y: number; } | -- |
IBfMapPolyline
Name | Description | Type | Default |
---|---|---|---|
points | 经纬度数组 | IBfMapPoint[] | (required) |
color | 线的颜色 | string | -- |
width | 线的宽度 | number | -- |
opacity | 线的透明度 | number | -- |