react-native-mapsを使ってReactNativeで現在地、緯度、経度を取得する。
マップロケーションを開発しています。ある特定の場所をクリックすると、緯度と経度が表示されますが、現在地の緯度と経度は表示されません。
調べる方法がわかりません。
どのようにしてそれらを入手し、どのようにしてその位置にマーカーを置くことができるのでしょうか?
以下は私のコードです。
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>
)
}
}
21
3
react-native@0.42.3
と
react-native-maps@^0.13.1を使用し、日付で
react-native@0.44.0と
react-native-maps@^0.15.2`を使用して、以下の手順で行いました。state
に
mapRegionオブジェクトを設定し、最後の
longitudeと最後の
latitudeを
null`にします。そして、
componentDidMount()
関数の中で、現在の位置の変化をそれぞれ監視します。変更があったら、実際の座標と
delta
値を渡して、this.state.mapRegion
で更新します (私のはあなたのとは違うかもしれないので、それに合わせてください)。次に、
onRegionChange()
関数が必要です。これは、componentDidMount()
関数の中で、要素に新しい値を設定するのに使われます。componentWillUnmount()`でジオロケーションをアンマウントします。
そして、現在の
mapRegion
オブジェクトを渡してMapView
をレンダリングします。その中のMapView.Marker
は、現在のlatitude
とlongitude
が変化したときに、それを表示するためのものです。StyleSheet.absoluteFillObject](https://til.hashrocket.com/posts/202dfcb6c4-absolute-fill-component-in-react-native)をマップに追加することで、デバイスの幅と高さ全体を使って適切にレンダリングすることができます。
そのため、
onPress()
関数では、onRegionChange()
と同様のことができます。expo.io](https://snack.expo.io/Sk7Ch1eae)でフルコードをチェックしてみてください(ただし、`react-native-maps`はインストールされていません)。
ジオローカリゼーションについては、こちらの公式ドキュメントをお読みになることをお勧めします: https://facebook.github.io/react-native/docs/geolocation.html
そして、現在の位置情報を使って、その情報を自分の状態に当てはめることができます。
次に render メソッドで、最終的なビューをマーカーで構成することができるようになります。
以下のコードでロケーションパーミッションを探します。
次のコードを使用して、現在の場所の緯度と経度を取得します。