这就是我正在使用的。我几乎获得了当前位置,但无法放置标记或图标,也无法显示位置名称。我也实现了下面的代码,但没有成功。如果有人有想法,那么请建议我
async getLocationAsync () {
// permissions returns only for location permissions on iOS and under certain conditions, see Permissions.LOCATION
const { status, permissions } = await Permissions.askAsync(
Permissions.LOCATION
);
if (status === 'granted') {
this.setState({ hasLocationPermissions: true });
// let location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
let location = await Location.getCurrentPositionAsync({});
this.setState({ locationResult: JSON.stringify(location) });
// Center the map on the location we just fetched.
this.setState({
mapRegion: {
latitude: location.coords.latitude,
longitude: location.coords.longitude,
latitudeDelta: 0.04,
longitudeDelta: 0.05,
},
});
} else {
alert('Location permission not granted');
}
};
render() {
return (
<View style={styles.container}>
<MapView
style={styles.mapStyle}
region={this.state.mapRegion}
onRegionChange={this.handleMapRegionChange}
/>
{/* <Image
source={{uri:'https://png.icons8.com/dusk/100/000000/compass.png'}}
style={{width: 100, height: 100, marginBottom:150}}
/> */}
<Ionicons style = {{paddingLeft: 0}} name = "md-locate" size = {30} color = "red"/>
<Text style = {styles.boldText}>
You are Here
</Text>
<Text style={{justifyContent:'center',alignItems: 'center',marginTop:20}}>
Longitude: {this.state.mapRegion.location.coords.longitude}
</Text>
<Text style={{justifyContent:'center',alignItems: 'center'}}>
Latitude: {this.state.mapRegion.location.coords.latitude}
</Text>
</View>
);
}
}