首页 > 其他分享 >使用 expo-location 获取不到位置信息 Location.getCurrentPositionAsync() 结果无打印

使用 expo-location 获取不到位置信息 Location.getCurrentPositionAsync() 结果无打印

时间:2023-10-05 16:44:31浏览次数:36  
标签:getCurrentPositionAsync text expo Location import location

问题描述:

react native 中使用 expo-location 获取位置信息时,按照官方文档执行案例,获取不到位置结果。Location.requestForegroundPermissionsAsync() 执行后可以获取授权状态,但是执行到 Location.getCurrentPositionAsync() 时不会打印结果。

解决方法:

给 Location.getCurrentPositionAsync() 添加参数,如下

let location = await Location.getCurrentPositionAsync({
  accuracy: Location.Accuracy.Highest,
  maximumAge: 10000,
});
console.log("Location:", location);

官方文档:

expo-location文档地址

  1. 安装:
npx expo install expo-location
  1. 使用:
import React, { useState, useEffect } from 'react';
import { Platform, Text, View, StyleSheet } from 'react-native';
import Device from 'expo-device';
import * as Location from 'expo-location';

export default function App() {
  const [location, setLocation] = useState(null);
  const [errorMsg, setErrorMsg] = useState(null);

  useEffect(() => {
    (async () => {
      if (Platform.OS === 'android' && !Device.isDevice) {
        setErrorMsg(
          'Oops, this will not work on Snack in an Android Emulator. Try it on your device!'
        );
        return;
      }
      let { status } = await Location.requestForegroundPermissionsAsync();
      if (status !== 'granted') {
        setErrorMsg('Permission to access location was denied');
        return;
      }

      let location = await Location.getCurrentPositionAsync({});
      setLocation(location);
    })();
  }, []);

  let text = 'Waiting..';
  if (errorMsg) {
    text = errorMsg;
  } else if (location) {
    text = JSON.stringify(location);
  }

  return (
    <View style={styles.container}>
      <Text style={styles.paragraph}>{text}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    padding: 20,
  },
  paragraph: {
    fontSize: 18,
    textAlign: 'center',
  },
});

官方案例中 getCurrentPositionAsync 方法传参为空对象 {} ,这是打印不出结果的原因。
添加参数后便可以获取结果数据。

标签:getCurrentPositionAsync,text,expo,Location,import,location
From: https://www.cnblogs.com/lpkshuai/p/17743517.html

相关文章

  • dcgm-exporter:Collect Switch Metrics和Collect Link Metrics
    在DCGM(DataCenterGPUManager)中,"CollectSwitchMetrics"和"CollectLinkMetrics"是两个功能选项,用于收集关于GPU交换机和连接的指标数据。它们的含义如下:CollectSwitchMetrics(收集交换机指标)在GPU集群中,GPU交换机是用于处理GPU设备之间通信和数据传输的关键组......
  • 5 分钟理解 Next.js SSG (Static Site Generation / Static Export)
    5分钟理解Next.jsSSG(StaticSiteGeneration/StaticExport)在本篇文章中,我们将介绍Next.js中的SSG(静态网站生成)功能,以及它是如何工作的。我们将介绍SSG的基本概念,以及在Next.js中如何使用ServerComponents和ClientComponents来实现不同的数据获取策略。什......
  • D365从云端UAT环境Export DB到本地开发环境
    1,导出数据参考微软的如下链接去操作,很详尽,最终得到一个”.bacpac”备份文件Exportacopyofthestandarduseracceptancetesting(UAT)database-Finance&Operations|Dynamics365|MicrosoftLearn2,导入数据尝试过用SSMS(MicrosoftSQLServerMamagement)导入......
  • 使用sqoop export命令将hive数据导入mysql
    描述一下因为我前两天的测试里面,用的是另外一种方法,所以今天想要尝试一下sqoopexport的方法,这个方法我之前也试过,但是一直报错,但是!我又来啦!!!相关步骤自己设置一个数量不多的csv文件:然后按照之前的步骤:上传,导入数据库:然后在mysql里面同时创建一个与hive数据库中结构一致......
  • 从安卓模拟器中获取 expo-av 库录音得到的音频文件 file:///data/user/0/mo.com.nccl.
    在使用expo-av录制音频时,录制结束通过recording.getURI()可以获取得到的音频文件的地址。想要获取该文件可以通过发送请求的方式:consturi=recording.getURI();letresponse=awaitfetch(uri);letblob=awaitresponse.blob();如果想直接根据文件路径找到这个文......
  • Docker - Expose a port
    Inordertoaccessthenginxfromourworkstation,wewould needtoexposetheport80fromwithinthenginxcontainertoourworkstation.Let usfirststopthecontainerandrecreateittoexposethecontainertoourworkstation:%dockerstopvigorous_......
  • selenium 报错 element not interactable: [object HTMLDivElement] has no size and
    selenium自动化识别验证码x,y坐标 命令move_to_element_with_offset报错:elementnotinteractable:[objectHTMLDivElement]hasnosizeandlocation由于>4.0是以中心点偏移,4.0是左上角偏移。卸载掉最新的seleniuim:pipuninstallselenium安装selenium4.0:pipinstalls......
  • RocketMQ教程-(6-5)-运维部署-Promethus Exporter
    介绍Rocketmq-exporter 是用于监控RocketMQbroker端和客户端所有相关指标的系统,通过 mqAdmin 从broker端获取指标值后封装成87个cache。警告过去版本曾是87个concurrentHashMap,由于Map不会删除过期指标,所以一旦有label变动就会生成一个新的指标,旧的无用指标无法......
  • android-解决 Android N 上 报错:android.os.FileUriExposedException
    我们使用手机的时候经常会看到应用程序提示升级,大部分应用内部都需要实现升级提醒和应用程序文件(APK文件)下载。一般写法都差不多,比如在启动app的时候,通过api接口获得服务器最新的版本号,然后和本地的版本号比较,来判断是否需要弹出提示框下载,当然也可以通过推送的自定义消息来实现......
  • export ‘Vue‘ (imported as ‘Vue‘) was not found in ‘vue‘
    export'Vue'(importedas'Vue')wasnotfoundin'vue'(possibleexports:$computed,$fromRefs,$raw,$ref,BaseTransition,Comment,EffectScope,Fragment,KeepAlive,ReactiveEffect,Static,Suspense,Teleport,Text,Transit......