React 组件中使用 百度地图 API,懒加载百度地图 SDK

这是一个基于 React 封装的百度地图组件,帮助你轻松的接入地图到 React 项目中,Gitee 镜像仓库

文档实例预览: Github | Gitee
在这里插入图片描述

特性

  • ♻️ 自动加载百度地图 SDK(通过创建 Script 标签的形式加载),包括第三方 SDK。
  • 📚 使用 Typescript 编写,集成百度地图 SDK @type 声明文件(包括中文注释)。
  • ⚛️ 支持 React Hook 新增特性(需要 React 16.8+)。
  • 💝 不依赖任何第三方组件。

安装

不依赖 uiw 组件库

npm install @uiw/react-baidu-map --save

使用

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

const Demo = () => (
  <div style={{ width: '100%', height: '300px' }}>
    <APILoader akay="GTrnXa5hwXGwgQnTBG28SHBubErMKm3f">
      <Map />
    </APILoader>
  </div>
);
ReactDOM.render(<Demo />, _mount_);

APILoader

用于加载百度地图 SDK 依赖,加载完成,全局将会有 window.BMap 对象,这个过程是通过创建 Script 标签的形式加载。

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

您需先申请密钥(ak)才可使用该服务,接口无使用次数限制,请开发者放心使用。

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

const Demo = () => (
  <div style={{ width: '100%', height: '300px' }}>
    <APILoader akay="GTrnXa5hwXGwgQnTBG28SHBubErMKm3f">
      <Map autoLocalCity />
    </APILoader>
  </div>
);
ReactDOM.render(<Demo />, _mount_);

PointCollection 加载海量点

表示海量点类,利用该类可同时在地图上展示万级别的点,目前仅适用于html5浏览器。

渲染海量点会耗费一定性能,应该注意不要频繁变动 point 数组

在这里插入图片描述

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

const Example = () => {
  const [visiable, setVisiable] = useState(true);
  const [points, usePoints] = useState([]);
  const [position, usePositon] = useState('');
  useEffect(() => {
    if(points.length === 0) {
      requireScript('https://lbsyun.baidu.com/jsdemo/data/points-sample-data.js').then(() => {
        if(window.data && window.data.data) {
          usePoints(window.data.data);
        }
      });
    }
  });
  function compRef(props) {
    if (props && props.pointCollection) {
      console.log('pointCollection::', props.pointCollection, props.map, props.BMap);
    }
  }
  return (
    <>
      <button onClick={() => setVisiable(!visiable)}>{visiable ? '隐藏' : '显示'}</button>
      {position && <span>标注点经纬度:{position}</span>}
      <Map widget={['NavigationControl']} zoom={5}>
        <PointCollection
          ref={compRef}
          visiable={visiable}
          onClick={(e) => {
            usePositon(JSON.stringify(e.point))
          }}
          styles={{ shape: 1 }}
          points={[
            ...points
          ]}
        />
      </Map>
    </>
  )
}

const Demo = () => (
  <div style={{ width: '100%', height: '350px' }}>
    <APILoader akay="GTrnXa5hwXGwgQnTBG28SHBubErMKm3f">
      <Example />
    </APILoader>
  </div>
);
ReactDOM.render(<Demo />, _mount_);

Polyline 折线组件

使用浏览器的矢量制图工具(如果可用)在地图上绘制折线的地图叠加层。

在这里插入图片描述

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

const Example = () => {
  return (
    <>
      <Map zoom={13} center="北京" widget={['NavigationControl']}>
        <Polyline
          enableEditing={false}
          strokeOpacity={0.9}
          path={[
            { lng: 116.399, lat: 39.910 },
            { lng: 116.405, lat: 39.920 },
            { lng: 116.423493, lat: 39.907445 },
          ]}
        />
        <Polyline
          enableEditing={false}
          strokeOpacity={0.9}
          path={[
            { lng: 116.399, lat: 39.920977 },
            { lng: 116.385243, lat: 39.913063 },
            { lng: 116.394226, lat: 39.917988 },
            { lng: 116.401772, lat: 39.921364 },
            { lng: 116.41248, lat: 39.927893 },
          ]}
        />
      </Map>
    </>
  );
}

const Demo = () => (
  <div style={{ width: '100%', height: '350px' }}>
    <APILoader akay="GTrnXa5hwXGwgQnTBG28SHBubErMKm3f">
      <Example />
    </APILoader>
  </div>
);
ReactDOM.render(<Demo />, _mount_);

更多组件以及文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小弟调调

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值