我正在使用Windows 10并正在运行Android 7.0设备。
我正在尝试通过地理位置使设备显示经纬度和当前位置的长状态-当用户按下按钮时。
我创建了一个按钮,并在用户单击该按钮时显示了一条警告消息-到目前为止,一切都很好。
然后我尝试添加获取当前位置,然后我的应用因错误而崩溃:
未定义不是对象(评估'_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',
}
});
Run Code Online (Sandbox Code Playgroud)
“未定义不是对象”是什么意思,我该如何解决?(当我尝试访问相机时,我遇到了同样的错误。)
React-Native(v 0.55)Geolocation文档说,
作为浏览器polyfill,可通过navigator.geolocation全局使用此API-无需导入。
不要Geolocation
从导入 react-native
。而是navigator.geolocation
直接使用哪个是全局对象。
例如,
navigator.geolocation.getCurrentPosition(
(position) => console.log(position),
(err) => console.log(err),
{ enableHighAccuracy: false, timeout: 8000, maximumAge: 10000 }
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3786 次 |
最近记录: |