Lav*_*aju 21 react-native react-native-android react-native-maps
我正在开发一个地图位置.当我在某个特定的地方点击时,我得到纬度和经度,但不是当前的位置,纬度和经度.
我不知道如何找出答案.
我怎样才能得到它们?如何将标记放在那个位置?
这是我的代码:
class Maps extends React.Component {
constructor(props) {
super(props);
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},
marker: {
latlng:{
latitude: null,
longitude: null,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
}
}
}
componentDidMount() {
navigator.geolocation.getCurrentPosition (
(position) => { alert("value:" + position) },
(error) => { console.log(error) },
{
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 10000
}
)
}
onMapPress(e) {
alert("coordinates:" + JSON.stringify(e.nativeEvent.coordinate))
this.setState({
marker: [{ coordinate: e.nativeEvent.coordinate }]
})
}
render() {
return (
<View style={styles.container}>
<View style={{flexGrow:1}}>
<MapView
ref="map"
provider={this.props.provider}
style={styles.map}
onPress={this.onMapPress.bind(this)}
provider = {PROVIDER_DEFAULT}
mapType="standard"
zoomEnabled={true}
pitchEnabled={true}
showsUserLocation={true}
followsUserLocation={true}
showsCompass={true}
showsBuildings={true}
showsTraffic={true}
showsIndoors={true}>
</MapView>
</View>
</View>
)
}
}
Run Code Online (Sandbox Code Playgroud)
Seb*_*lma 33
我做了以下步骤使用react-native@0.42.3和react-native-maps@^0.13.1使用react-native@0.44.0,并react-native-maps@^0.15.2于本公布日期:
设置一个mapRegion对象中state,最后longitude,最后latitude为null:
state = {
mapRegion: null,
lastLat: null,
lastLong: null,
}
Run Code Online (Sandbox Code Playgroud)
然后在您的componentDidMount()函数中监视当前位置的每个更改:
componentDidMount() {
this.watchID = navigator.geolocation.watchPosition((position) => {
...
});
}
Run Code Online (Sandbox Code Playgroud)
当有更改时更新它们this.state.mapRegion,传递实际的坐标和delta值(我的可能与你的不同,所以调整它们):
componentDidMount() {
this.watchID = navigator.geolocation.watchPosition((position) => {
// Create the object to update this.state.mapRegion through the onRegionChange function
let region = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.00922*1.5,
longitudeDelta: 0.00421*1.5
}
this.onRegionChange(region, region.latitude, region.longitude);
}, (error)=>console.log(error));
}
Run Code Online (Sandbox Code Playgroud)
然后你需要这个onRegionChange()函数,用于为函数中的元素"设置"新值componentDidMount():
onRegionChange(region, lastLat, lastLong) {
this.setState({
mapRegion: region,
// If there are no new values set the current ones
lastLat: lastLat || this.state.lastLat,
lastLong: lastLong || this.state.lastLong
});
}
Run Code Online (Sandbox Code Playgroud)
卸载地理位置componentWillUnmount():
componentWillUnmount() {
navigator.geolocation.clearWatch(this.watchID);
}
Run Code Online (Sandbox Code Playgroud)
并渲染MapView传递当前mapRegion对象,其MapView.Marker内部只是向您显示当前latitude和longitude何时更改:
render() {
return (
<View style={{flex: 1}}>
<MapView
style={styles.map}
region={this.state.mapRegion}
showsUserLocation={true}
followUserLocation={true}
onRegionChange={this.onRegionChange.bind(this)}>
<MapView.Marker
coordinate={{
latitude: (this.state.lastLat + 0.00050) || -36.82339,
longitude: (this.state.lastLong + 0.00050) || -73.03569,
}}>
<View>
<Text style={{color: '#000'}}>
{ this.state.lastLong } / { this.state.lastLat }
</Text>
</View>
</MapView.Marker>
</MapView>
</View>
);
}
Run Code Online (Sandbox Code Playgroud)
为地图添加StyleSheet.absoluteFillObject,以便使用设备的整个宽度和高度正确渲染它.
const styles = StyleSheet.create({
map: {
...StyleSheet.absoluteFillObject,
}
});
Run Code Online (Sandbox Code Playgroud)
因此,对于您的onPress()函数,您可以执行与onRegionChange()获取实际坐标并设置它们类似的操作:
onMapPress(e) {
let region = {
latitude: e.nativeEvent.coordinate.latitude,
longitude: e.nativeEvent.coordinate.longitude,
latitudeDelta: 0.00922*1.5,
longitudeDelta: 0.00421*1.5
}
this.onRegionChange(region, region.latitude, region.longitude);
}
Run Code Online (Sandbox Code Playgroud)
检查expo.io上的完整代码(虽然react-native-maps未安装)
小智 5
我建议您阅读有关地理定位的官方文档:https ://facebook.github.io/react-native/docs/geolocation.html
然后,使用当前位置,您可以将该信息放入您的状态:
navigator.geolocation.getCurrentPosition((position) => {
this.setState({position: {longitude: position.longitude, latitude: position.latitude}});
}, (error) => {
alert(JSON.stringify(error))
}, {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 1000
});
Run Code Online (Sandbox Code Playgroud)
接下来,您将能够在渲染方法中使用标记来组成最终视图:
render() {
return (
<MapView ...>
<MapView.Marker
coordinate={this.state.position}
title="title"
description="description"
/>
</MapView>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47388 次 |
| 最近记录: |