gpt4 book ai didi

javascript - 如何从本地的 Location.getCurrentPositionAsync() 结果中获取经纬度

转载 作者:行者123 更新时间:2023-11-30 09:16:10 24 4
gpt4 key购买 nike

这是我获取设备当前位置的代码。

import React, { Component } from 'react';
import { Platform, Text, View, StyleSheet, FlatList } from 'react-native';
import { Constants, Location, Permissions, MapView } from 'expo';

export default class Home extends Component {

state = {
location: null,
errorMessage: null,
};

componentWillMount() {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
});
} else {
this._getLocationAsync();
}
}

_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
}

let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
this.setState({ location });
};

render() {
let text = 'Waiting..';
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.location) {
text = JSON.stringify(this.state.location);
}
console.log(text)
return (
<MapView
style={{ flex: 1 }}
region={{
latitude: text.coords.latitude,
longitude: text.coords.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
}}
/>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
textAlign: 'center',
},
});

我想做的是将纬度和对数传递给 MapView。但这不起作用。

的输出console.log(text)

{
"timestamp":1551594077000,
"mocked":false,
"coords":{
"heading":0,
"longitude":80.4380389,
"speed":0,
"altitude":-78,
"latitude":6.0140343,
"accuracy":21.238000869750977

}

我正在使用 expo 应用程序在我的智能手机 (galaxy j5 10) 中运行这个项目。所有位置权限都已授予该应用程序,但仍无法正常工作。我尝试了很多文档,但没有用。我该如何纠正这个。

最佳答案

您的错误是由您的渲染方法引起的:

render() {
let text = 'Waiting..';
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.location) {
text = JSON.stringify(this.state.location);
}
console.log(text)
return (
<MapView
style={{ flex: 1 }}
region={{
latitude: text.coords.latitude,
longitude: text.coords.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
}}
/>
);
}

this.state.errorMessage 为空时,您没有为 this.state.location 设置值,因此您的 MapView 会尝试使用您设置为 text 的值,这不起作用,因为 this.state.location 为 null,如果您尝试访问其上的值,将会抛出错误。

当您获得位置时,您可以使用 JSON.stringify 将位置对象转换为字符串,但这会阻止您访问该对象的属性。

this.state.errorMessagethis.state.location 都为空时,您的 text 只是一个字符串,因此它会导致MapView 出错,因为您正试图访问字符串上的对象属性。

你应该这样做:

  1. 设置加载状态的初始值
  2. _getLocationAsync 中设置加载状态
  3. 仅在获得许可后才检查位置
  4. 重构渲染,使其处理组件的加载(它应该显示 3 种不同输出之一,未加载、加载但有错误、已加载但有位置)

这里是重构

export default class Home extends Component {
state = {
location: null,
errorMessage: null,
loaded: false
};
// componentWillMount has been deprecated, use componentDidMount instead
componentDidMount () {
if (Platform.OS === 'android' && !Constants.isDevice) {
this.setState({
errorMessage: 'Oops, this will not work on Sketch in an Android emulator. Try it on your device!',
loaded:true
});
} else {
this._getLocationAsync();
}
}

_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
loaded: true
});
} else {
// only check the location if it has been granted
// you also may want to wrap this in a try/catch as async functions can throw
let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
this.setState({ location, loaded: true, errorMessage: null });
}
};

render () {
// check to see if we have loaded
if (this.state.loaded) {
// if we have an error message show it
if (this.state.errorMessage) {
return (
<View style={styles.container}>
<Text>{JSON.stringify(this.state.errorMessage)}</Text>
</View>
);
} else if (this.state.location) {
// if we have a location show it
return (
<MapView
style={{ flex: 1 }}
region={{
latitude: this.state.location.coords.latitude,
longitude: this.state.location.coords.longitude,
latitudeDelta: 0.1,
longitudeDelta: 0.1
}}
/>
);
}
} else {
// if we haven't loaded show a waiting placeholder
return (
<View style={styles.container}>
<Text>Waiting...</Text>
</View>
);
}
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1'
},
paragraph: {
margin: 24,
fontSize: 18,
textAlign: 'center'
}
});

关于javascript - 如何从本地的 Location.getCurrentPositionAsync() 结果中获取经纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54966296/

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