android - 添加地理位置以创建 React Native App
问题描述
我使用最新的 Expo CLI 使用React Native 文档中的说明创建了我的第一个 React Native 应用程序:
npm install -g expo-cli
npm expo init AwesomeProject
后来我想将地理定位库添加到这个应用程序中,似乎整个自动链接不适合我。我使用了提供的说明:
yarn add @react-native-community/geolocation
而且我还没有完成手动链接,因为我在当前最新版本中使用 React Native(如库的自述文件中所述,高于 0.59)。添加示例代码后:
import Geolocation from '@react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
我已经开始收到应该链接库的错误。手动调用链接命令后,不幸的是它仍然没有工作。
解决方案
实际上,我跳出了这些库并对此进行了完整的博览会(但我仍然不确定这是否是正确的方法):
import { useState, useEffect } from "react";
import * as Permissions from "expo-permissions";
import * as Location from "expo-location";
export const useLocation = () => {
const [location, setLocation] = useState();
useEffect(() => {
async function setLocationWithPerms() {
const permissions = await Permissions.askAsync(Permissions.LOCATION);
if (permissions.status === "granted") {
const currentLocation = await Location.getCurrentPositionAsync({});
setLocation(currentLocation);
}
}
setLocationWithPerms();
}, []);
return location;
};
推荐阅读
- javascript - Feathers js在邮递员中使用原始json上传文件
- typescript - 使用类验证器动态设置验证组
- python - matplotlib savefig() 看起来比 show() 差
- javascript - 背景动画在css中放大和缩小幻灯片
- spark-streaming - 增加 Kafka Streams 消费者吞吐量
- php - 结帐字段编辑器插件过滤器钩子为结帐字段设置自定义显示规则
- postgresql - 在 PostgreSQL 上创建一个计算分类变量的列的最简单方法是什么?也许某种旋转?
- javascript - 如何更改文本动画以便它们只显示在自己的空间中?
- performance - 在 Loadrunner 中,有没有办法并行运行 HTML 请求?
- blockchain - 如何从超级账本中的中介接收消息