gpt4 book ai didi

react-native - React Native Undefined 不是对象 - 地理定位

转载 作者:行者123 更新时间:2023-12-04 05:12:33 25 4
gpt4 key购买 nike

我正在使用 Windows 10 并运行 Android 7.0 设备。

我试图让设备使用地理定位显示其当前位置的经纬度 - 当用户按下按钮时。

我创建了一个按钮,并在用户单击它时显示一条警告消息 - 到目前为止一切顺利。

然后我尝试添加获取当前位置,然后我的应用程序因错误而崩溃:

undefined is not an object (evaluating '_reactNative.Geolocation.getCurrentPosition')



这是我的代码:
import React, { Component } from 'react';
import {
Platform,
StyleSheet,
Text,
View,
Button,
Alert,
Geolocation
} from 'react-native';

const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});

export default class App extends Component {
render() {
return (
<View style={styles.container}>

<Button
onPress={() => {
var loc = Geolocation.getCurrentPosition(geo_success, [geo_error], [geo_options]);
Alert.alert(loc);

//Alert.alert('You tapped the button!');
}}
title="Display GPS"
/>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});

“未定义不是对象”是什么意思,我该如何解决? (我之前尝试访问相机时遇到了同样的错误。)

最佳答案

更新:该功能已从 react-native 核心中删除,因此以下答案不再无效。
React-Native (v 0.55) 地理定位文档说,

As a browser polyfill, this API is available through thenavigator.geolocation global - you do not need to import it.


请勿导入 Geolocation来自 react-native .而是使用 navigator.geolocation直接这是一个全局对象。
例如,
navigator.geolocation.getCurrentPosition(
(position) => console.log(position),
(err) => console.log(err),
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 10000 }
);

关于react-native - React Native Undefined 不是对象 - 地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49814630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com