簡體   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