All Projects → huiyan-fe → react-bmapgl

huiyan-fe / react-bmapgl

Licence: MIT license
基于百度地图JavaScript GL版API封装的React组件库

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-bmapgl

react-native-s-baidumap
百度地图 React Native ,同时支持ios和android
Stars: ✭ 28 (-58.82%)
Mutual labels:  map, baidumap
Vue Baidu Map
Baidu Map components for Vue 2.x
Stars: ✭ 2,191 (+3122.06%)
Mutual labels:  map, baidumap
React Leaflet
React components for Leaflet maps
Stars: ✭ 3,939 (+5692.65%)
Mutual labels:  map, react-components
gobaidumap
百度地图接口调用 golang 版。支持GEO、地址双向获取,IP获取地址。Baidu Map for golang.
Stars: ✭ 38 (-44.12%)
Mutual labels:  map, baidumap
LittleNavmapOFMTheme
Open Flightmaps VFR Map Theme for Little Navmap
Stars: ✭ 34 (-50%)
Mutual labels:  map
mars2d
【Mars2D平台 】主仓库,包含所有开源仓库清单导航
Stars: ✭ 182 (+167.65%)
Mutual labels:  map
UMapControl
轻量级跨平台瓦片地图库
Stars: ✭ 35 (-48.53%)
Mutual labels:  map
arboles
Mapa de Arbolado Urbano
Stars: ✭ 13 (-80.88%)
Mutual labels:  map
AhaAlgorithms
《啊哈算法》书上代码
Stars: ✭ 47 (-30.88%)
Mutual labels:  map
Fable.SemanticUI
React.SeamanticUI to Fable bindings
Stars: ✭ 15 (-77.94%)
Mutual labels:  react-components
gmap
heterogenous Map over a GADT
Stars: ✭ 40 (-41.18%)
Mutual labels:  map
EQEmuMaps
EQEmuMap .map and .path reference files for the internal EQEmu Server functions
Stars: ✭ 15 (-77.94%)
Mutual labels:  map
covid-19
Current and historical coronavirus covid-19 confirmed, recovered, deaths and active case counts segmented by country and region. Includes csv, json and sqlite data along with an interactive website explorer.
Stars: ✭ 15 (-77.94%)
Mutual labels:  map
dashkit-ui
UI Components built on React.
Stars: ✭ 17 (-75%)
Mutual labels:  react-components
react-carousel-3d
3D carousal component in react
Stars: ✭ 129 (+89.71%)
Mutual labels:  react-components
MapMoves
View your Moves app and Arc app/LocoKit location history summary on map
Stars: ✭ 35 (-48.53%)
Mutual labels:  map
material-react-components
React components implementing the Material Design specification
Stars: ✭ 21 (-69.12%)
Mutual labels:  react-components
transit
Massively real-time city transit streaming application
Stars: ✭ 20 (-70.59%)
Mutual labels:  map
ustd
Micro-standard-library providing minimal and portable array, queue and map for attiny avr, arduinos, esp8266/32 and linux, mac
Stars: ✭ 14 (-79.41%)
Mutual labels:  map
vscode-react-javascript-snippets
Extension for React/Javascript snippets with search supporting ES7+ and babel features
Stars: ✭ 782 (+1050%)
Mutual labels:  react-components

React-BMapGL

npm version Package Quality Download License

基于百度地图JavaScript GL版API封装的React组件库。如果想要使用旧版的2D地图的话,使用react-bmap。如果您对使用地图API完全陌生,建议使用这个库之前先了解百度地图JavaScript GL API,了解一些地图的基本概念,并申请开发者ak


文档示例

官方地址:http://huiyan.baidu.com/github/react-bmapgl/

备用地址:https://huiyan-fe.github.io/react-bmapgl/


开始使用

引入脚本

首先,需要在你的index.html模板页面头部加载百度地图JavaScript API代码,密钥可去百度地图开放平台官网申请

<script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=您的密钥"></script>

然后,使用npm方式安装react组件库,然后通过es模块加载

npm install react-bmapgl --save

Hello World

import React from 'react';
import ReactDOM from 'react-dom';
import {Map, Marker, NavigationControl, InfoWindow} from 'react-bmapgl';

class App extends React.Component {
    render() {
        return <Map center={{lng: 116.402544, lat: 39.928216}} zoom="11">
            <Marker position={{lng: 116.402544, lat: 39.928216}} />
            <NavigationControl /> 
            <InfoWindow position={{lng: 116.402544, lat: 39.928216}} text="内容" title="标题"/>
        </Map>
    }
}
ReactDOM.render(<App />, document.getElementById('container'));

获取map实例

如果你在业务中需要操作map对象,需要BMapGL.Map实例的话,可以通过<Map>组件实例的map属性访问到它。

<Map ref={ref => {this.map = ref.map}} />

如果你要开发Map的子组件,想要在子组件中获得map对象,可以直接在<Map>包裹的子组件中调用this.props.map即可。

function App() {
    return <Map>
        <MapComponent />
    </Map>
}

function MapComponent(props) {
    console.log(props.map);
}

异步加载与按需导入

异步加载

通常,如果您的业务场景总需要使用到地图,或者需要创建多个地图实例,我们会建议您在index.html模板中加入JSAPI的scirpt标签,因为这种方式对后续开发没有任何限制。
但是在某些业务场景下,可能并不一定会使用到地图,且用到的地图功能、组件也相对简单,我们也提供了MapApiLoaderHOC高阶组件,实现异步加载的方式。

class App extends React.Component {
  render() {
    return (
      <Map
        style={{ height: 450 }}
        center={new BMapGL.Point(116.404449, 39.914889)}
        zoom={12}
      />
    )
  }
}

export default MapApiLoaderHOC({ak: '您的密钥'})(App)

按需导入

常用的导入方法会直接把整个包导入进来

import { Map, Marker, MapvglView, MapvglLayer } from 'react-bmapgl'

如果希望引入的包体积小一点,所有组件都支持lodash风格进行按需导入

import Map from 'react-bmapgl/dist/Map'
import Marker from 'react-bmapgl/dist/Overlay/Marker'

本地开发

设计思想

React-BMapGL只是利用了React组件的生命周期,来调用对应的百度地图JavaScript API的方法,比如在componentDidMount的时候在地图上添加覆盖物,componentWillUnmount的时候移除覆盖物,componentDidUpdate的时候更新覆盖物,React对应的render渲染函数模块返回的是null。所以这里面地图相关的DOM并不是react渲染的,真正创建地图之类的还是使用百度地图JavaScript API,React-BMapGL只是利用了React组件的写法来封装百度地图JavaScript API,使我们在使用React的时候能更方便的使用百度地图JavaScript API。

Typescript支持

本项目开发使用Typescript编写,声明文件为@types/bmapgl,如果需要修改,需要给DefinitelyTyped提PR。如果需要安装声明文件依赖,执行安装命令

npm install @types/bmapgl -D

常用命令

npm install         # 安装依赖

npm start           # 开始运行文档网站
npm run build:doc   # 文档网站构建编译

npm run build       # 组件库编译,输出 js 和 .d.ts 文件

npm publish         # 发布新npm包

发布npm包

修改package.json版本号后,直接执行npm publish


许可证

MIT LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].