我发现世博会的位置文档的代码:
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>
);
}
}
通过返回我的纬度,经度,准确性时间戳和其他的东西工作正常,我。
可有人告诉我,我怎样才能得到我的坐标,并将其呈现在地图上?我想使用的纬度,经度的状态,并通过它从我的位置访问到该状态的值,以进一步把它放入的MapView的初始区域
人谁可以帮助?
我知道这个答案是有点晚了,但万一有人通过它运行,这是我做的:
新增的区域属性状态:
state = {
location: null,
region: null,
errorMessage: null,
};
在的getLocations,我翻译了getCurrentPositionAsync()调用的区域返回的定位对象:
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 });
};
终于在渲染方法我用过的状态下的区域属性设置区域中的MapView:
render() {
return (
<MapView
initialRegion={this.state.region}
/>
);
}
一旦异步调用,以获得位置完成后,地图跳转到当前位置。