最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native Geolocation GetCurrentPosition EnableHighAccuracy - Stack Overflow

programmeradmin2浏览0评论

I am using Geolocation on Android to get a user's position. I am a little bit confused about the EnableHighAccuracy setting. Basically, to make this work I have to set it to "true" for Android Simulator and to "false" for a physical device. Otherwise its broken and I get timeout error and no location.

Can someone please clarify why this might be the case? It seems strange that this one setting pletely breaks it when it should not. I don't know if this has perhaps something to do with device settings or something else. Seems a bit dangerous for production with this being so hacky. Thanks.

navigator.geolocation.getCurrentPosition(
 async (locationObj) => {
   //Some code
 },
 (error => Alert.alert("Could not get location"),
 { enableHighAccuracy: true, timeout: 15000 }
)

I am using Geolocation on Android to get a user's position. I am a little bit confused about the EnableHighAccuracy setting. Basically, to make this work I have to set it to "true" for Android Simulator and to "false" for a physical device. Otherwise its broken and I get timeout error and no location.

Can someone please clarify why this might be the case? It seems strange that this one setting pletely breaks it when it should not. I don't know if this has perhaps something to do with device settings or something else. Seems a bit dangerous for production with this being so hacky. Thanks.

navigator.geolocation.getCurrentPosition(
 async (locationObj) => {
   //Some code
 },
 (error => Alert.alert("Could not get location"),
 { enableHighAccuracy: true, timeout: 15000 }
)
Share Improve this question edited Sep 21, 2019 at 10:19 user9846301 asked Sep 12, 2019 at 19:30 CosmicSeizureCosmicSeizure 1,5245 gold badges20 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

if you set "enableHighAccuracy" to true then it will use GPS and location will be accurate .

This is a bug in Geolocation . On Android it will timeout . if you want accurate location and want to enableHighAccuracy then you should use react-native-geolocation-service

As described in library

"This library is created in an attempt to fix the location timeout issue on android with the react-native's current implementation of Geolocation API."

Also remended in official site of React Native

"On Android, this uses the android.location API. This API is not remended by Google because it is less accurate and slower than the remended Google Location Services API. In order to use it with React Native, use the react-native-geolocation-service module."

Try this

...
import Geolocation from 'react-native-geolocation-service';
...

ponentDidMount() {
    // Instead of navigator.geolocation, just use Geolocation.
    if (hasLocationPermission) {
        Geolocation.getCurrentPosition(
            (position) => {
                console.log(position);
            },
            (error) => {
                // See error code charts below.
                console.log(error.code, error.message);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
        );
    }
}
发布评论

评论列表(0)

  1. 暂无评论