繁体   English   中英

如何使用 React Native 在 Mapview 中显示当前位置?

[英]How do I show the current location in a Mapview using React Native?

在我的场景中,我想获取用户的当前位置并在该当前位置的 map 上显示一个引脚。 我试过了,但我遇到了以下问题:

TypeError:未定义不是对象(评估'navigator.geolocation.getCurrentPosition')

我该如何解决上述问题?

您还可以使用 RNLocation package 获取用户位置,我已与您共享代码。 您只需要安装 package。

  const [location, setLocation] = useState(null);

useEffect(()=> {
    RNLocation.configure({
        distanceFilter: 5.0
    })

    RNLocation.requestPermission({
        ios: "whenInUse",
        android: {
            detail: "coarse"
        }
    }).then(granted => {
        if (granted) {
            RNLocation.subscribeToLocationUpdates(locations => {
                setLocation(locations)
            })
        }
    })
},[])

暂无
暂无

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

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