要使用@react-native-community/geolocation,首先需要安装它。

  1. 在项目根目录下,运行以下命令来安装@react-native-community/geolocation:
npm install @react-native-community/geolocation

或者使用 yarn:

yarn add @react-native-community/geolocation
  1. 安装完成后,运行以下命令来链接到原生项目:
npx pod-install
  1. 在需要使用@react-native-community/geolocation的文件中,引入它:
import Geolocation from '@react-native-community/geolocation';
  1. 使用Geolocation提供的方法来获取设备的地理位置信息。例如,你可以使用getCurrentPosition方法来获取当前位置的经纬度:
Geolocation.getCurrentPosition(
  position => {
    console.log(position.coords.latitude, position.coords.longitude);
  },
  error => {
    console.log(error);
  },
  { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);

上述代码将获取设备的当前位置,并在控制台打印经纬度。

这是一个基本的使用示例,你还可以查看@react-native-community/geolocation的文档来了解更多的方法和配置选项。


原文地址: https://www.cveoy.top/t/topic/i4cW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录