2.7.1 • Published 4 months ago

@uiw/react-baidu-map-geolocation-control v2.7.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

GeolocationControl 定位控件

Buy me a coffee npm version Downloads

负责进行地图定位的控件,使用html5浏览器定位功能。你也可以使用 Map 组件 widget 属性来设置控件更方便。

import { GeolocationControl, useGeolocationControl } from '@uiw/react-baidu-map';
// 或者单独安装使用
import GeolocationControl, { useGeolocationControl } from '@uiw/react-baidu-map-geolocation-control';

基本用法

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

const Example = () => {
  const [show, setShow] = useState(true);
  return (
    <>
      <button onClick={() => setShow(!show)}>
        {show ? '关闭' : '开启'}
      </button>
      <Map zoom={13} style={{ height: 350 }}>
        {show && (
          <GeolocationControl
            onLocationSuccess={(e) => {
              let address = '';
              address += e.addressComponent.province;
              address += e.addressComponent.city;
              address += e.addressComponent.district;
              address += e.addressComponent.street;
              address += e.addressComponent.streetNumber;
              alert("当前定位地址为:" + address);
              console.log('address', address, e);
            }}
          />
        )}
        <GeolocationControl
          offset={new BMap.Size(40, 40)}
          anchor={BMAP_ANCHOR_TOP_RIGHT}
          visiable={show}
          onLocationSuccess={(opt) => {
            console.log('opt', opt);
          }}
          onLocationError={(StatusCode) => {
            console.log('StatusCode', StatusCode);
          }}
        />
      </Map>
    </>
  );
}

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

使用 hooks

geolocationControl, setGeolocationControl

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

const Example = () => {
  const divElm = useRef(null);
  const [show, setShow] = useState(true);
  const { setContainer, map } = useMap();
  const { geolocationControl } = useGeolocationControl({
    map, anchor: BMAP_NAVIGATION_CONTROL_LARGE, visiable: show
  });

  useEffect(() => {
    if (divElm.current && !map) {
      setContainer(divElm.current);
    }
  }, [map]);
  return (
    <>
      <button onClick={() => setShow(!show)}>
        {show ? '关闭' : '开启'}
      </button>
      <div ref={divElm} style={{ height: 350 }} />
    </>
  );
}

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

Props

参数说明类型默认值
visiable覆盖物是否可见。boolean-
anchor控件的位置偏移值。ControlAnchorBMAP_ANCHOR_TOP_RIGHT
offset控件的水平偏移值。BMap.Size-
showAddressBar是否显示定位信息面板。默认显示定位信息面板。boolean-
enableAutoLocation添加控件时是否进行定位。默认添加控件时不进行定位。boolean-
locationIcon可自定义定位中心点的 Icon 样式。Icon-

事件

参数说明类型
onLocationSuccess定位成功后触发此事件(event: { point: BMap.Point, addressComponent: BMap.AddressComponent }) => void;
onLocationError定位失败后触发此事件(event: { statusCode: BMap.StatusCode }) => void;

NavigationControlType

常量描述
BMAP_NAVIGATION_CONTROL_LARGE0标准的平移缩放控件(包括平移、缩放按钮和滑块)
BMAP_NAVIGATION_CONTROL_SMALL1仅包含平移和缩放按钮
BMAP_NAVIGATION_CONTROL_PAN2仅包含平移按钮
BMAP_NAVIGATION_CONTROL_ZOOM3仅包含缩放按钮
2.7.1

4 months ago

2.7.0

5 months ago

2.6.3

5 months ago

2.6.2

9 months ago

2.6.1

12 months ago

2.6.0

1 year ago

2.5.5

1 year ago

2.5.4

1 year ago

2.5.3

2 years ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.3.0

2 years ago

2.1.2

2 years ago

2.2.0

2 years ago

2.1.1

2 years ago

2.4.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.1.0

2 years ago

2.0.6

2 years ago

2.0.5

3 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago