react-baidu-maps
Features
- React components for Baidu Maps.
- Custom config supported.
Installation
npm install react-baidu-maps --save
Usage
Getting Started
You need to obtain a Baidu Maps AK through Baidu LBS Platform.
Then import Baidu Maps script into your index.html
.
Use Baidu Maps statically:
;ReactDOM;
Use Baidu Maps dynamically with asynchronous load:
;const AsyncMap = ;ReactDOM;
Handle Events
All events are mapped to React props with name like 'on + {event name}'(e.g. tilesloaded => onTilesloaded, resize => onResize),the first letter of event name should be capitalized.
ReactDOM;
Overlays
All Baidu Maps overlays are supported.
Marker
<BaiduMap mapUrl=`http://api.map.baidu.com/api?v=2.0&ak=` loadingElement=<div>Loading...</div> mapContainer=<div style= height: '100%' /> > <Marker position= lng: 116404 lat: 39915 /></BaiduMap>
Circle
<Circle center= lng: 116404 lat: 39915 radius=500 strokeColor="red" strokeWeight=2 />
Curve
const polygon = lng: 116387112 lat: 39920977 lng: 116394226 lat: 39917988 lng: 116401772 lat: 39921364 lng: 11641248 lat: 39927893 ;<Curve path=polygon strokeWeight=2 strokeColor="red" />
GroundOverlay
<Ground bounds= sw: lng: 116424319 lat: 39907408 ne: lng: 116442285 lat: 39914714 imageUrl="http://lbsyun.baidu.com/jsdemo/img/si-huan.png" />
Label
<Label position= lng: 116365139 lat: 39916595 content="Label Demo" offset= width: 30 height: -30 />
Polygon
const polygon = lng: 116387112 lat: 39920977 lng: 116394226 lat: 39917988 lng: 116401772 lat: 39921364 lng: 11641248 lat: 39927893 ;<Polygon path=polygon strokeWeight=2 />
Polyline
const polyline = lng: 116399 lat: 39910 lng: 116405 lat: 39920 lng: 116425 lat: 39900 ;<Polyline path=polyline strokeWeight=2 strokeColor="green" />
Custom Overlay
<Overlay constructorParams=... customConstructor=... initialize=... draw=... />
MarkerClusterer
const MAX = 30;const markerClusterer = ;for let i = 0; i < MAX; i++ markerClusterer; <MarkerClusterer> markerClusterer</MarkerClusterer>
InfoWindow
<Marker position= lng: 116404 lat: 39915 > <InfoWindow content="marker infoWindow" offset= width: 0 height: -20 /></Marker>
CanvasLayer
<CanvasLayer zIndex=10 update= { const ctx = canvas; if !ctx return; ctx; ctxfillStyle = 'rgba(50, 50, 255, 0.7)'; ctx; const data = 116297047 39979542 // eslint-disable-line no-undef 116321768 3988748 // eslint-disable-line no-undef 116494243 39956539 // eslint-disable-line no-undef ; for let i = 0 len = datalength; i < len; i++ const pixel = thismap; ctx; }/>
Controls
All Baidu Maps controls are supported.
NavigationControl
<BaiduMap mapUrl=`http://api.map.baidu.com/api?v=2.0&ak=` loadingElement=<div>Loading...</div> mapContainer=<div style= height: '100%' /> > <NavigationControl type="small" anchor="top_right" offset= width: 0 height: 30 /></BaiduMap>
ScaleControl
<ScaleControl />
MapTypeControl
<MapTypeControl />
OverviewMapControl
<OverviewMapControl />
GeolocationControl
<GeolocationControl onLocationSuccess= { let address = ''; address += eaddressComponentprovince; address += eaddressComponentcity; address += eaddressComponentdistrict; address += eaddressComponentstreet; address += eaddressComponentstreetNumber; console; } />
CopyrightControl
<CopyrightControl anchor="bottom_right" copyrights= id: 1 content: "<div href='#' style='font-size:20px;background:yellow'>我是自定义版权控件</div>" bounds: sw: lng: 116055026 lat: 39591042 ne: lng: 116752974 lat: 40237421 />
Subway
<AsyncMap mapUrl=`http://api.map.baidu.com/api?type=subway&v=1.0&ak=` loadingElement=<div style=textAlign: 'center' fontSize: 40>Loading...</div> id='asyncmap1' mapContainer=<div style=height: '100%' /> city='289'/>
Subway Maker
<SubwayMarker station='西直门' icon= url: 'https://api.map.baidu.com/images/subway/start-bak.png' size: width: 50 height: 80 />
Subway Zoom Control
<SubwayZoomControl anchor='top_right'/>