6.0.3 • Published 1 month ago

@uiw/react-amap-geolocation v6.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Geolocation 定位

Buy me a coffee npm version Downloads

鹰眼控件,用于显示缩略地图,显示于地图右下角,可以随主图的视口变化而变化,也可以配置成固定位置实现类似于南海附图的效果。

import { Geolocation } from '@uiw/react-amap';
// 或者单独安装使用
import { Geolocation } from '@uiw/react-amap-geolocation';

地图定位控件

import React, { useState, useRef } from 'react';
import { APILoader, Map, Geolocation } from '@uiw/react-amap';

const Example = () => {
  const [data, setData] = useState();
  return (
    <>
      <div style={{ width: '100%', height: '300px' }}>
        <Map zoom={4}>
          <Geolocation
            // 是否使用高精度定位,默认:true
            enableHighAccuracy={true}
            // 超过10秒后停止定位,默认:5s
            timeout={10000}
            // 定位按钮的停靠位置
            // 官方 v2 不再支持
            // buttonPosition="RB"

            // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
            // 官方 v2 不再支持
            // buttonOffset={new AMap.Pixel(10, 20)}
            
            // 定位成功后是否自动调整地图视野到定位点
            zoomToAccuracy={true}
            onComplete={(data) => {
              console.log('返回数据:', setData, data);
              setData(data);
            }}
            onError={(data) => {
              console.log('错误返回数据:', data);
              setData(data);
            }}
          />
        </Map>
      </div>
      <pre style={{ padding: 10, marginTop: 10 }}>
        {data ? JSON.stringify(data, null, 2) : '{正在获取}'}
      </pre>
    </>
  );
}

const Mount = () => (
  <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129">
    <Example />
  </APILoader>
);

export default Mount;

只获取定位经纬度

import React, { useState, useRef } from 'react';
import { APILoader, Geolocation } from '@uiw/react-amap';

const Example = () => {
  const [data, setData] = useState();
  return (
    <>
      <div style={{ width: '100%' }}>
        <Geolocation
          // 是否使用高精度定位,默认:true
          enableHighAccuracy={true}
          // 超过10秒后停止定位,默认:5s
          timeout={10000}
          // 定位按钮的停靠位置
          buttonPosition="RB"
          // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
          buttonOffset={new AMap.Pixel(10, 20)}
          // 定位成功后是否自动调整地图视野到定位点
          zoomToAccuracy={true}
          onComplete={(data) => {
            console.log('返回数据:', data);
            setData(data);
          }}
          onError={(data) => {
            console.log('错误返回数据:', data);
            setData(data);
          }}
        />
        <pre style={{ padding: 10, marginTop: 10 }}>
          {data ? JSON.stringify(data, null, 2) : '{正在获取}'}
        </pre>
      </div>
    </>
  );
}

const Mount = () => (
  <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129">
    <Example />
  </APILoader>
);

export default Mount;

只获取定位地址信息

import React, { useState, useRef } from 'react';
import { APILoader, Geolocation } from '@uiw/react-amap';

const Example = () => {
  const [data, setData] = useState();
  return (
    <>
      <div style={{ width: '100%' }}>
        <Geolocation
          type="cityInfo"
          // 是否使用高精度定位,默认:true
          enableHighAccuracy={true}
          // 超过10秒后停止定位,默认:5s
          timeout={10000}
          // 定位按钮的停靠位置
          buttonPosition="RT"
          // 定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
          buttonOffset={new AMap.Pixel(10, 20)}
          // 定位成功后是否自动调整地图视野到定位点
          zoomToAccuracy={true}
          onComplete={(data) => {
            console.log('返回数据:', data);
            setData(data);
          }}
          onError={(data) => {
            console.log('错误返回数据:', data);
            setData(data);
          }}
        />
        <pre style={{ padding: 10, marginTop: 10 }}>
          {data ? JSON.stringify(data, null, 2) : '{正在获取}'}
        </pre>
      </div>
    </>
  );
}

const Mount = () => (
  <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129">
    <Example />
  </APILoader>
);

export default Mount;

不使用组件

import ReactDOM from 'react-dom';
import React, { useEffect, useState, useRef } from 'react';
import { APILoader, Geolocation } from '@uiw/react-amap';

const Example = () => {
  const [data, setData] = useState();
  useEffect(() => {
    AMap.plugin(['AMap.Geolocation'], () => {
      const instance = new AMap.Geolocation({});
      instance.getCityInfo((status, result) => {
        console.log('>>>>', status, result)
        if(status === 'complete'){
          setData(result);
        } else {
          setData(result);
        }
      });
    });
  }, []);
  return (
    <>
      <div style={{ width: '100%' }}>
        <pre style={{ padding: 10, marginTop: 10 }}>
          {data ? JSON.stringify(data, null, 2) : '{正在获取}'}
        </pre>
      </div>
    </>
  );
}

const Mount = () => (
  <APILoader akey="a7a90e05a37d3f6bf76d4a9032fc9129">
    <Example />
  </APILoader>
);

export default Mount;

Props

参数说明类型默认值
type获取 position: "获取 用户的精确位置,有失败几率" 或 cityInfo: "根据用户 IP 获取 用户所在城市信息"position\|cityInfoposition

更多 API 参考

事件

参数说明类型
onComplete数据请求完成时触发事件。(data: GeolocationLiveResult \| GeolocationForecastResult): void;
onError数据请求错误时触发事件。(err): void;
6.0.3

1 month ago

6.0.2

3 months ago

6.0.1

5 months ago

6.0.0

5 months ago

5.0.20

5 months ago

5.0.21

5 months ago

5.0.9

9 months ago

5.0.8

10 months ago

5.0.7

10 months ago

5.0.6

10 months ago

5.0.5

10 months ago

5.0.10

9 months ago

5.0.11

9 months ago

5.0.12

8 months ago

5.0.13

8 months ago

5.0.14

8 months ago

5.0.15

7 months ago

5.0.16

7 months ago

5.0.17

7 months ago

5.0.18

6 months ago

5.0.19

6 months ago

5.0.4

11 months ago

5.0.3

11 months ago

5.0.2

11 months ago

4.5.3

1 year ago

4.6.0

1 year ago

5.0.1

1 year ago

5.0.0

1 year ago

4.4.1

1 year ago

4.4.0

1 year ago

4.4.2

1 year ago

4.5.0

1 year ago

4.5.2

1 year ago

4.5.1

1 year ago

4.0.5

1 year ago

4.2.2

1 year ago

4.0.7

1 year ago

4.0.6

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.3.1

1 year ago

4.1.0

1 year ago

4.3.0

1 year ago

4.1.1

1 year ago

4.0.4

1 year ago

4.0.3

1 year ago

4.0.1

2 years ago

4.0.2

2 years ago

4.0.0

2 years ago

3.0.4

2 years ago

2.7.22

2 years ago

2.7.21

2 years ago

2.7.20

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

2.7.6

2 years ago

2.7.5

2 years ago

3.0.0

2 years ago

2.7.7

2 years ago

2.7.9

2 years ago

2.7.19

2 years ago

2.7.18

2 years ago

2.7.17

2 years ago

2.7.16

2 years ago

2.7.11

2 years ago

2.7.10

2 years ago

2.7.15

2 years ago

2.7.14

2 years ago

2.7.13

2 years ago

2.7.12

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.5.0

2 years ago

2.4.1

2 years ago

2.7.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.1

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.6.2

2 years ago

2.3.0

3 years ago

2.4.0

3 years ago

2.2.6

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago