简体   繁体   English

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

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

In my scenario, I want to get the user's current location and show a pin on a map of that current location.在我的场景中,我想获取用户的当前位置并在该当前位置的 map 上显示一个引脚。 I tried but I'm facing the issue below:我试过了,但我遇到了以下问题:

TypeError: undefined is not an object( evaluating 'navigator.geolocation.getCurrentPosition') TypeError:未定义不是对象(评估'navigator.geolocation.getCurrentPosition')

How do I resolve the above issue?我该如何解决上述问题?

You can also use RNLocation package to get user location I have shared the code with you.您还可以使用 RNLocation package 获取用户位置,我已与您共享代码。 You just need to install that 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