Rez*_*oor 2 javascript google-maps react-native react-native-maps react-hooks
有很多关于如何在类组件中使用react-native-maps的示例,但我还没有找到那些带有无状态组件的示例。我使用无状态组件来访问全局状态。在获得用户的位置后,我需要将 MapView 的相机视图从初始区域移动,在互联网上搜索后,我不知怎的最终得到了这段不起作用的代码。
import React, { useState, useEffect, useRef } from 'react'
import MapView, { Marker, PROVIDER_GOOGLE } from 'react-native-maps';
...
const Explore = props => {
const [userCoords, setUserCoords] = useState({
latitude: 0,
longitude: 0
})
const mapRef = useRef(null);
onMapReady = () => {
getUserLocation()
}
getUserLocation = async () => {
Geolocation.getCurrentPosition(
async (position) => {
setUserCoords(position.coords)
mapRef.animateToRegion({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
})
},
async (error) => {
// See error code charts below.
console.log("Geolocation Error" + error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000 }
);
}
return(
...
<MapView
ref={mapRef}
onMapReady={() => onMapReady()}
region={{
latitude: userCoords.latitude,
longitude: userCoords.latitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
showsUserLocation={true}>
...
</MapView>
)
...
export default Explore
Run Code Online (Sandbox Code Playgroud)
请大家帮帮我~谢谢
| 归档时间: |
|
| 查看次数: |
3431 次 |
| 最近记录: |