Map

Map 地图【H5】

地图组件(仅适用于H5,使用leaflet)。组件属性尽量对齐 Taro 的 Map 组件

与 Taro 的 Map 组件的差异点:

  1. marker 的 iconPath 只支持传网络图片或者通过 import 导入的图片,无法直接传本地路径
  2. marker 的 anchor 属性对应 anchorX、anchorY,且仅当有传入 width、height 时有效
  3. polyline 的 color 不支持 alpha 通道,要设置透明度请使用 opacity 属性
  4. 由于高德坐标系和 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
// showLocation
style={{ 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);
}}
/>
<Button
onClick={() => {
setLatLng({ lat: 31.232454, lng: 121.476607 });
}}
>
move to 来福士广场
</Button>
<Button
onClick={() => {
setLatLng({ lat: 31.228234, lng: 121.475497 });
}}
>
move to 上海博物馆
</Button>
</View>
);
};

API

IBfMapProps

NameDescriptionTypeDefault
longitude中心经度number--
latitude中心纬度number--
scale缩放级别number16
minScale最小缩放级别number--
maxScale最大缩放级别 max:18number--
showLocation显示当前定位点booleanfalse
markers标记点IBfMapMarker[]--
polyline路线IBfMapPolyline[]--
onMarkerTap点击标记点时触发,e.detail = {markerId}IBfMapCommonEventFunction<onMarkerTapEventDetail, {}>--
onCalloutTap点击标记点对应的气泡时触发,e.detail = {markerId}IBfMapCommonEventFunction<onCalloutTapEventDetail, {}>--
onRegionChange视野发生变化时触发IBfMapCommonEventFunction<TOnRegionChangeDetailType, { mpEvent: TOnRegionChangeDetailType; }>--

IBfMapMarker

NameDescriptionTypeDefault
id标记点idstring | 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

NameDescriptionTypeDefault
points经纬度数组IBfMapPoint[](required)
color线的颜色string--
width线的宽度number--
opacity线的透明度number--
dumi10:24