Geolocation API在Firefox 66.0.3中返回NaN问题求助
Hey there, let's figure out why your geolocation code is returning NaN in Firefox 66.0.3 while working fine in other browsers and older Firefox versions.
First, your core code logic looks solid—you're correctly checking for the Geolocation API existence, setting options, and calling getCurrentPosition. The issue is almost certainly tied to a specific bug in Firefox 66.x's handling of the Geolocation API, especially when enableHighAccuracy is set to true.
Possible Causes
- High Accuracy Mode Bug: Firefox 66.x had known inconsistencies with the
enableHighAccuracy: truesetting. In some cases, if the device couldn't access a high-precision location source (like GPS), the browser failed to fall back to lower-precision network-based location, resulting in NaN values instead of an error or valid coordinates. - Permission Reset: Sometimes browser updates can reset location permissions—double-check that Firefox 66.0.3 has permission to access your location for the site.
- Signal Issues: High accuracy mode relies heavily on GPS signals—if you're testing indoors, Firefox 66.x might struggle to get a fix where older versions would fall back to network location.
Fixes to Try
1. Fall Back to Low Accuracy Mode on Failure
Modify your code to handle errors and retry with enableHighAccuracy: false if the first request fails. This works around the Firefox 66.x bug by falling back to a more reliable (though less precise) location method:
function init() { if(navigator.geolocation) { // First attempt with high accuracy const highAccuracyOpts = { enableHighAccuracy: true, maximumAge: 300000 }; navigator.geolocation.getCurrentPosition(show_pos, handleGeoError, highAccuracyOpts); } } function show_pos(position) { console.log(position.coords.latitude, position.coords.longitude); } function handleGeoError(error) { console.error(`Geolocation failed: ${error.message}`); // Retry with low accuracy if high accuracy failed if ([error.POSITION_UNAVAILABLE, error.TIMEOUT].includes(error.code)) { const lowAccuracyOpts = { enableHighAccuracy: false, maximumAge: 300000 }; navigator.geolocation.getCurrentPosition(show_pos, finalError, lowAccuracyOpts); } } function finalError(error) { console.error(`Final geolocation failure: ${error.message}`); }
2. Disable High Accuracy Temporarily
If you don't strictly need high-precision coordinates, try setting enableHighAccuracy: false directly in your initial options. This skips the problematic high accuracy mode entirely and should return valid coordinates in Firefox 66.0.3:
function init() { if(navigator.geolocation) { const geoOptions = { enableHighAccuracy: false, maximumAge: 300000 }; navigator.geolocation.getCurrentPosition(show_pos, error_pos, geoOptions); } } function show_pos(position) { console.log(position.coords.latitude, position.coords.longitude); } function error_pos(error) { console.error(`Geolocation error: ${error.message}`); }
3. Verify Location Permissions
- Open Firefox 66.0.3's settings
- Go to Privacy & Security > Permissions > Location
- Ensure your site is listed as "Allowed"
Additional Notes
This issue was likely a regression in Firefox 66.x that was fixed in later versions. If possible, encouraging users to update their Firefox would also resolve the problem, but the fallback code above ensures compatibility with the problematic version.
内容的提问来源于stack exchange,提问作者Kebs Villarojo

