New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@uiw/react-baidu-map-map

Package Overview
Dependencies
Maintainers
0
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uiw/react-baidu-map-map

Baidu Map Components for React.

latest
Source
npmnpm
Version
2.7.5
Version published
Weekly downloads
2.9K
15.87%
Maintainers
0
Weekly downloads
 
Created
Source
使用 我的应用 也是一种 支持 我的方式:
Deskmark Keyzer Vidwall Hub VidCrop Vidwall Mousio Hint Mousio Musicer Audioer FileSentinel FocusCursor Videoer KeyClicker DayBar Iconed Mousio Quick RSS Quick RSS Web Serve Copybook Generator DevTutor for SwiftUI RegexMate Time Passage Iconize Folder Textsound Saver Create Custom Symbols DevHub Resume Revise Palette Genius Symbol Scribe

Map 组件

Buy me a coffee npm version Downloads

Map 组件是其他组件的基础,Map 组件会给所有的子组件注入两个属性 mapcontainerBMap,在 v2.3.0+ 版本支持使用 useMapContext 获取它们。

🚧 注意

  • 组件 <Map> 必须包裹在 <APILoader> 组件内,该组件作用是加载百度地图 SDK。
  • 其他地图组件必须作为 <Map> 的子组件使用;
import { Map, useMap, APILoader, Provider, useMapContext } from '@uiw/react-baidu-map';
// 或者单独安装使用
import Map, { useMap, APILoader, Provider, useMapContext } from '@uiw/react-baidu-map-map';

🚧 注意:需要使用到 <Provider>,新的封装 Marker 组建使用了 Context 来避免 map 对象传来传去。

基本用法

Map 的父组件必须具有宽度和高度;

import React from 'react';
import { Map, APILoader } from '@uiw/react-baidu-map';

const Demo = () => (
  <div style={{ width: '100%', height: '300px' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Map />
    </APILoader>
  </div>
);
export default Demo;

小工具

百度地图上负责与地图交互的UI元素称为控件。百度地图API中提供了丰富的控件,您还可以通过 Control 类来实现自定义控件,百度控件文档

import React from 'react';
import { Map, APILoader } from '@uiw/react-baidu-map';

const Demo = () => (
  <div style={{ width: '100%', height: '300px' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Map
        ref={(props) => {
          if (props && props.map) {
            // 启用滚轮放大缩小,默认禁用
            props.map.enableScrollWheelZoom();
          }
        }}
        widget={[
          'GeolocationControl',
          {
            name: 'OverviewMapControl',
            options: {
              isOpen: true,
            }
          },
          {
            name: 'CopyrightControl',
            control: (BMap, map) => {
              // 设置版权控件位置
              const cr = new BMap.CopyrightControl({ anchor: BMAP_ANCHOR_TOP_RIGHT });
              // 返回地图可视区域
              const bs = map.getBounds();
              cr.removeCopyright(1);
              cr.addCopyright({
                id: 1,
                content: "<a href='#' style='font-size:20px;background:yellow'>我是自定义版权控件呀</a>",
                // bounds: bs,
              });
              return cr;
            },
          },
          {
            name: 'NavigationControl',
            options: (BMap) => {
              return {
                offset: new BMap.Size(150, 5),
                showZoomInfo: false,
                enableGeolocation: true,
              }
            }
          }
        ]}
      />
    </APILoader>
  </div>
);
export default Demo;

useMapContext & Provider

通过 React 的 Context 提供了一个无需为每层组件手动注入 mapcontainerBMap 三个属性 props,就能在组件树间进行传递。

🚧 在 v2.3.0+ 版本支持

import React from 'react';
import { useState, useRef, useEffect } from 'react';
import { Map, APILoader, useMap, Provider, useMapContext } from '@uiw/react-baidu-map';

const Marker = () => {
  const { map } = useMapContext();
  const container = useRef(null);
  const { setContainer } = useMap({
    zoom: 3,
    widget: ['GeolocationControl', 'NavigationControl']
  });

  useEffect(() => {
    if (container.current) {
      setContainer(container.current);
    }
  }, [container.current]);

  useEffect(() => {
    if (map) {
      // 创建点标记
      const marker1 = new BMap.Marker(new BMap.Point(116.404, 39.925));
      const marker2 = new BMap.Marker(new BMap.Point(116.404, 39.915));
      const marker3 = new BMap.Marker(new BMap.Point(116.395, 39.935));
      const marker4 = new BMap.Marker(new BMap.Point(116.415, 39.931));
      // 在地图上添加点标记
      map.addOverlay(marker1);
      map.addOverlay(marker2);
      map.addOverlay(marker3);
      map.addOverlay(marker4);
    }
  }, [map]);
  return (
    <div ref={container} style={{ height: 300 }} />
  );
}

const Demo = () => {
  return (
    <div style={{ width: '100%', height: '300px', overflow: 'auto' }}>
      <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
        <Provider>
          <div>
            <Marker />
          </div>
        </Provider>
      </APILoader>
    </div>
  );
}
export default Demo;
import { useMapContext, Provider } from '@uiw/react-baidu-map';

const { BMap, map, container, state, dispatch } = useMapContext();
// => state.BMap
// => state.map
// => state.container

可控属性

import React from 'react';
import { useState } from 'react';
import { Map, APILoader } from '@uiw/react-baidu-map';

const Demo = () => {
  const [center, setCenter] = useState('北京');
  const [enableScrollWheelZoom, setEnableScrollWheelZoom] = useState(true);
  return (
    <div style={{ width: '100%', height: '300px' }}>
      <button onClick={() => setCenter('北京')}>北京</button>
      <button onClick={() => setCenter('上海')}>上海</button>
      <button onClick={() => setEnableScrollWheelZoom(!enableScrollWheelZoom)}>{enableScrollWheelZoom ? '禁用': '启用'}滚轮放大缩小</button>
      <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
        <Map enableScrollWheelZoom={enableScrollWheelZoom} zoom={10}  center={center}/>
      </APILoader>
    </div>
  );
}
export default Demo;

使用 hooks

map, setMap, container, setContainer, zoom, setZoom, setCenter, autoLocalCity, setAutoLocalCity,

import React from 'react';
import { useRef, useEffect, useState } from 'react';
import { Map, APILoader, useMap } from '@uiw/react-baidu-map';

const Example = () => {
  const divElm = useRef();
  const [zoom, setZoom] = useState(15)
  const { setContainer, map, center, setCenter, setAutoLocalCity } = useMap({
    enableScrollWheelZoom: true, // 启用滚轮放大缩小,默认禁用
    center: '北京',
    widget: ['GeolocationControl', 'NavigationControl']
  });
  useEffect(() => {
    if (divElm.current && !map) {
      setContainer(divElm.current);
    }
  });

  useEffect(() => {
    if (map) {
      // 启用滚轮放大缩小,默认禁用
      map.setZoom(zoom);
    }
  }, [zoom, map]);
  
  return (
    <>
      <button onClick={() => setZoom(zoom-1)}>-</button>
      <span>{zoom || 15}</span>
      <button onClick={() => setZoom(zoom+1)}>+</button>
      <button onClick={() => setCenter('北京')}>北京</button>
      <button onClick={() => setCenter('上海')}>上海</button>
      <button onClick={() => setAutoLocalCity(true)}>IP定位</button>
      <div ref={divElm} style={{ height: 350 }} />
    </>
  )
}

const Demo = () => (
  <div style={{ width: '100%' }}>
    <APILoader akay="eYpCTECSntZmw0WyoQ7zFpCRR9cpgHFG">
      <Example />
    </APILoader>
  </div>
);
export default Demo;

Props

参数说明类型默认值
widget百度地图上负责与地图交互的UI元素称为控件,实例参考。百度控件文档string[]/object[][]
center定位, 可使用如 上海市青浦区 的地区字符串,也可以使用对象如 {lng: 121.424333, lat: 31.228604} 表示经纬度。百度拾取坐标系统Point,String上海市
autoLocalCity设为 true, IP定位获取当前城市,进行自动定位boolean-
zoom缩放等级,如果 center 类型为 Point 时,zoom必须赋值,范围 3-19 级, 若调用高清底图(针对移动端开发)时,zoom 可赋值范围为 3-18 级。 如果center类型为字符串时,比如“北京”,zoom可以忽略,地图将自动根据 center 适配最佳 zoom 级别number-
minZoom地图允许展示的最小级别number-
maxZoom地图允许展示的最大级别number-
mapType地图类型MapTypeBMAP_NORMAL_MAP
mapStyle设置地图样式MapStyle-
coordsType指定输入输出的坐标类型CoordsType-
defaultCursor设置地图默认的鼠标指针样式。参数 cursor 应符合 CSS 的 cursor 属性规范string-
draggingCursor设置拖拽地图时的鼠标指针样式。参数 cursor 应符合 CSS 的 cursor 属性规范string-
panorama将全景实例与Map类进行绑定Panorama-
currentCity设置地图城市,注意当地图初始化时的类型设置为 BMAP_NORMAL_MAP 时,需要在调用 centerAndZoom 之前调用此方法设置地图所在城市。例如: var map = new BMap.Map(“container”, {mapType: BMAP_NORMAL_MAP}); map.setCurrentCity(“北京市”); map.centerAndZoom(new BMap.Point(116.404, 39.915), 18); 注意:初始化的坐标应与您设置的城市对应,否则地图将无法正常显示。string-
viewport根据提供的地理区域或坐标设置地图视野,调整后的视野会保证包含提供的地理区域或坐标(view: Array<Point> | Viewport, viewportOptions: ViewportOptions)-
mapStyleV2设置地图样式的个性化地图服务V2,使用对象字面量形式表示,样式Json,通过编辑器生成。(支持Canvas){ styleJson: MapStyleItem[] } / {styleId: string}-
enableHighResolution是否启用使用高分辨率地图。在iPhone4及其后续设备上,可以通过开启此选项获取更高分辨率的底图,v1.2,v1.3版本默认不开启,v1.4 默认为开启状态boolean-
enableAutoResize是否启用自动适应容器尺寸变化,默认启用booleantrue
enableMapClick是否开启底图可点功能booleantrue
enableDragging启用地图拖拽,默认启用boolean-
enableScrollWheelZoom启用滚轮放大缩小,默认禁用boolean-
enableDoubleClickZoom启用双击放大,默认启用booleantrue
enableInertialDragging启用地图惯性拖拽,默认禁用boolean-
enableContinuousZoom启用连续缩放效果,默认禁用boolean-
enablePinchToZoom启用双指操作缩放,默认启用booleantrue
enableKeyboard启用键盘操作,默认禁用。键盘的上、下、左、右键可连续移动地图。同时按下其中两个键可使地图进行对角移动。PgUp、PgDn、Home和End键会使地图平移其1/2的大小。+、-键会使地图放大或缩小一级boolean-

事件

参数说明类型
onClick左键单击地图时触发此事件。 当双击时,产生的事件序列为: click click dblclick(event: {type: string, target: any, point: Point, pixel: Pixel, overlay: Overlay }): void;
onDblClick鼠标双击地图时会触发此事件(event: {type: string, target: any, pixel: Pixel, point: Point }): void;
onRightClick右键单击地图时触发此事件。 当双击时,产生的事件序列为: rightclick rightclick rightdblclick(event: {type: string, target: any, point: Point, pixel: Pixel, overlay: Overlay }): void;
onRightdblClick右键双击地图时触发此事件(event: {type: string, target: any, point: Point, pixel: Pixel, overlay: Overlay }): void;
onMapTypeChange地图类型发生变化时触发此事件(event: {type: string, target: any }): void;
onMouseMove鼠标在地图区域移动过程中触发此事件(event: {type: string, target: any, point: Point, pixel: Pixel, overlay: Overlay }): void;
onMouseOver鼠标移入地图区域时触发此事件(event: {type: string, target: any }): void;
onMouseOut鼠标移出地图区域时触发此事件(event: {type: string, target: any }): void;
onMoveStart地图移动开始时触发此事件(event: {type: string, target: any }): void;
onMoving地图移动过程中触发此事件(event: {type: string, target: any }): void;
onMoveEnd地图移动结束时触发此事件(event: {type: string, target: any }): void;
onZoomStart地图更改缩放级别开始时触发触发此事件(event: {type: string, target: any }): void;
onZoomEnd地图更改缩放级别结束时触发触发此事件(event: {type: string, target: any }): void;
onAddOverlay当使用 Map.addOverlay() 方法向地图中添加单个覆盖物时会触发此事件(event: {type: string, target: any }): void;
onAddControl当使用 Map.addControl() 方法向地图中添加单个控件时会触发此事件(event: {type: string, target: any }): void;
onRemoveControl当使用 Map.removeControl() 方法移除单个控件时会触发此事件(event: {type: string, target: any }): void;
onRemoveOverlay当使用 Map.removeOverlay() 方法移除单个覆盖物时会触发此事件(event: {type: string, target: any }): void;
onClearOverlays当使用 Map.clearOverlays() 方法一次性移除全部覆盖物时会触发此事件(event: {type: string, target: any }): void;
onDragStart开始拖拽地图时触发(event: {type: string, target: any, pixel: Pixel, point: Point }): void;
onDragging拖拽地图过程中触发(event: {type: string, target: any, pixel: Pixel, point: Point }): void;
onDragEnd停止拖拽地图时触发(event: {type: string, target: any, pixel: Pixel, point: Point }): void;
onAddTileLayer添加一个自定义地图图层时触发此事件(event: {type: string, target: any }): void;
onRemoveTileLayer移除一个自定义地图图层时触发此事件(event: {type: string, target: any }): void;
onLoad调用Map.centerAndZoom()方法时会触发此事件。这表示位置、缩放层级已经确定,但可能还在载入地图图块(event: {type: string, target: any, pixel: Pixel, point: Point, zoom: number }): void;
onReSize地图可视区域大小发生变化时会触发此事件(event: {type: string, target: any, size: Size }): void;
onHotspotClick点击热区时触发此事件(event: {type: string, target: any, spots: HotspotOptions }): void;
onHotspotOver鼠标移至热区时触发此事件(event: {type: string, target: any, spots: HotspotOptions }): void;
onHotspotOut鼠标移出热区时触发此事件(event: {type: string, target: any, spots: HotspotOptions }): void;
onTilesLoaded当地图所有图块完成加载时触发此事件(event: {type: string, target: any }): void;
onTouchStart触摸开始时触发此事件,仅适用移动设备(event: {type: string, target: any, point: Point, pixel}): void;
onTouchMove触摸移动时触发此事件,仅适用移动设备(event: {type: string, target: any, point: Point, pixel}): void;
onTouchEnd触摸结束时触发此事件,仅适用移动设备(event: {type: string, target: any, point: Point, pixel}): void;
onLongPress长按事件,仅适用移动设备(event: {type: string, target: any, point: Point, pixel}): void;

Keywords

react-baidu-map

FAQs

Package last updated on 12 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts