我目前正在学习 react-native,并且我正在开发一个使用 Geolocation 来查找和更新我当前位置的应用程序,我正在按照教程进行操作,并且在教程中我必须使用具有 timeout 和 maximumAge 选项。
navigator.geolocation.getCurrentPosition((pozitie)=>{
var lat = parseFloat(position.coords.latitude)
var long = parseFloat(position.coords.longitude)
// create an object with the current position taken from the geolocation chip
var initialLocation = {
latitude = lat,
longitude = long,
// Delta is the viewing angle on the location of the user
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
// Now we are seting initial position to the initial location
this.setState({initialPositionOnTheMap: initialLocation})
// we set the marker to the initial location and then the display follows the marker
// by using the initialLocaiton object that will receive the users current location
this.setState({positionOfTheMarker: initialLocation})
},
// make a function call if an error happens
// we make an allert that parses the error message
(error) => alert(JSON.stringify(error)),
// propertie
{enableHighAccuracy: true, timeout: 2000, maximumAge: 1000})
但是没有解释timeout和maximumAge是什么意思,我们为什么要用它们。
我知道这些选项在 react-native 提供的两种方法中使用:getCurrentPosition(调用最新位置的回调)和 watchPosition(每当位置发生变化时调用回调)但我不明白它们是什么准确地做。
最佳答案
我问过教程的作者,他的回答是:
超时:是一个正值,表示允许设备返回位置的最长时间。
MaximumAge:是一个正值,表示可接受返回的可能缓存位置的最长存在时间(以毫秒为单位)。换句话说,在我开始获取要返回的当前位置之前缓存的位置可以保留多久。
关于geolocation - React-Native 地理定位中的超时和maximumAge是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45167315/