繁体   English   中英

使用 function 组件通过 react native mapbox 获取当前坐标

[英]Get current coords with react native mapbox using function components

我正在使用带有 mapbox 库的本机反应,我正在执行以下操作来为用户获取 map 上的当前位置:

   <MapboxGL.MapView          
          surfaceView={true}
          zoomLevel={15}
          logoEnabled={false}
          style={styles.matchParent}>

           <MapboxGL.UserLocation
                visible={true}
                onUpdate={() => onUserLocationUpdate()}
            />

            <MapboxGL.Camera
                followZoomLevel={17} //followUserLocation
                followUserMode={'normal'}
                followUserLocation={true}
                followZoomLevel={17}
                animationDuration={1000}
            />


     </MapboxGL.MapView>

根据文档,道具onUpdate是在使用当前坐标进行位置更新时触发的,因此一旦触发,我将调用onUserLocationUpdate()执行以下操作:

const onUserLocationUpdate = async (location) => {
        //console.log(location)
        let lat = location.coords.latitude;
        let long = location.coords.longitude;
        let alt = location.coords.altitude;
        setlatitude(lat)
        setlongitude(long)
        setaltitude(alt)
} 

但是,我收到此错误:

错误类型错误:未定义不是 object(评估“location.coords”)

我正在使用反应本机 function 组件,但是,我在这里按照这个示例使用 class 组件,它对我有用,但是当我转换为 function 组件时,我得到了错误

那是 JS 的东西,与 Mapbox 无关。 尝试将location参数传递给您的 function ,例如:

onUpdate={(location) => onUserLocationUpdate(location)}

甚至更好:

onUpdate={onUserLocationUpdate}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM