我需要使用 javascript 高精度地定位我的用户。
目前我正在使用这个库 https://github.com/onury/geolocator
有以下选项
var options = {
enableHighAccuracy: true,
timeout: 15000,
maximumWait: 15000, // max wait time for desired accuracy
maximumAge: 0, // disable cache
desiredAccuracy: 30, // meters
fallbackToIP: false, // fallback to IP if Geolocation fails or rejected
};
它有点慢,但大部分时间都运行良好。
但我时不时遇到地理定位问题,我真的不明白为什么,但输出是:
{
"latitude": 40.712742950000006,
"longitude": -74.00595024412516,
"formattedAddress": "New York City Hall, 260, Broadway, Civic Center, Manhattan Community Board 1, Manhattan, New York County, New York, 10000, United States of America",
"country": "United States of America",
"city": "Manhattan Community Board 1",
"state": "New York",
"zipcode": "10000",
"streetName": "Broadway",
"streetNumber": "260",
"countryCode": "US",
"neighbourhood": "",
"provider": "openstreetmap",
}
似乎是默认位置,但脚本应该使用浏览器位置功能。</p>
所有用户都在移动设备上执行脚本
最佳答案
将超时选项设置为 Infinity
以避免在位置不可用(尚未)时返回。
HTML5 position timeout setting in milliseconds. Setting this to Infinity means that Geolocator won't return until the position is available.
参见:https://onury.io/geolocator/?api=geolocator#geolocator.locate
关于javascript - 定位用户 javascript 返回默认地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61823100/