Location 从世博会地点获取lat和long,以便在地图上获取
我在世博会的位置文档中找到了以下代码:Location 从世博会地点获取lat和long,以便在地图上获取,location,android-mapview,expo,Location,Android Mapview,Expo,我在世博会的位置文档中找到了以下代码: 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
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({});
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);
}
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View>
);
}
}
状态={
位置:空,
errorMessage:null,
};
组件willmount(){
if(Platform.OS==='android'&&!Constants.isDevice){
这是我的国家({
errorMessage:'哦,这在Android中的草图上不起作用
emulator。在您的设备上试用它!',
});
}否则{
这是。_getLocationAsync();
}
}
_getLocationAsync=async()=>{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
如果(状态!=“已授予”){
这是我的国家({
errorMessage:'访问位置的权限被拒绝',
});
}
let location=await location.getCurrentPositionAsync({});
this.setState({location});
};
render(){
让text='Waiting..';
if(this.state.errorMessage){
text=this.state.errorMessage;
}else if(this.state.location){
text=JSON.stringify(this.state.location);
}
返回(
{text}
);
}
}
它通过返回我的lat、long、精度时间戳和其他东西对我来说很好
有人能告诉我如何获得坐标并在地图上渲染吗??我想使用lat,long的状态,并将它从我的位置访问的值传递到该状态,以进一步将其放入mapview的初始区域
任何能提供帮助的人???我知道这个答案有点晚了,但万一有人碰到它,我就是这么做的: 已将区域属性添加到状态:
state = {
location: null,
region: null,
errorMessage: null,
};
在getLocations中,我将getCurrentPositionAsync()调用返回的location对象转换为一个区域:
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({});
const region = {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 1.0,
longitudeDelta: 1.0
}
this.setState({ location, region });
};
最后,在render方法中,我使用状态的region属性在mapview中设置区域:
render() {
return (
<MapView
initialRegion={this.state.region}
/>
);
}
render(){
返回(
);
}
完成获取位置的异步调用后,映射将跳转到当前位置