Baidu Map control Components for React.
npm install @uiw/react-baidu-map-control
Control 自定义控件
===



您可以通过此组件来实现自定义控件。同时你可以使用 Map 组件 widget 属性来更方便,设置自带默认控件。
``jsx`
import { Control, useControl } from '@uiw/react-baidu-map';
// 或者单独安装使用
import Control, { useControl } from '@uiw/react-baidu-map-control';
`jsx mdx:preview
import React, { useState, useRef } from 'react';
import { Map, Control, APILoader } from '@uiw/react-baidu-map';
const Example = () => {
const [count, setCount] = useState(0);
const map = useRef(null);
return (
const Demo = () => (
export default Demo;
`
control, setControl
`jsx mdx:preview
import React from 'react';
import { useMemo, useRef, useEffect, useState } from 'react';
import { Map, Provider, APILoader, useMap, useControl, useMapContext } from '@uiw/react-baidu-map';
const Example = () => {
const divElm = useRef(null);
const [count, setCount] = useState(4);
const { map } = useMapContext();
const { ControlPortal } = useControl({ anchor: BMAP_ANCHOR_TOP_RIGHT });
const { ControlPortal: ControlPortal2 } = useControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT });
return (
<>
onClick={() => {
setCount(count + 1);
map.setZoom(map.getZoom() + 1);
}}
>
放大1级 {count}
onClick={() => {
setCount(count - 1);
map.setZoom(map.getZoom() - 2);
}}
>
缩小1级 {count}
style={{ background: 'gray', padding: '10px', fontSize: 12 }}
onClick={() => setCount(count + 1)}
>
Current Count: {count}
const Demo = () => (
export default Demo;
`
| 参数 | 说明 | 类型 | 默认值 |
| ----- | ----- | ----- | ----- |
| visiable | 覆盖物是否可见。 | boolean | - |ControlAnchor
| anchor | 控件的停靠位置。| | BMAP_ANCHOR_TOP_LEFT |BMap.Size
| offset | 控件的位置偏移值。 | | new BMap.Size(10, 10) |JSX.Element
| children | 展示控件内容。 | | - |
此常量表示控件的定位。
| 常量 | 值 | 描述 |
| ----- | ----- | ----- |
| BMAP_ANCHOR_TOP_LEFT | 0 | ↖ 控件将定位到地图的左上角 |1
| BMAP_ANCHOR_TOP_RIGHT | | ↗ 控件将定位到地图的右上角 |2
| BMAP_ANCHOR_BOTTOM_LEFT | | ↙ 控件将定位到地图的左下角 |3
| BMAP_ANCHOR_BOTTOM_RIGHT | | ↘` 控件将定位到地图的右下角 |