I have a code snippet like this in a NextJS component:
const [currentGPS, setCurrentGPS] =
useState({coords:{latitude:0.0,longitude:0.0}})
useEffect(() => {
utl.getGPSLocation(
(v:{coords: {latitude:number; longitude:number;};})=>
setCurrentGPS(v))
}, [])
Whenever I try to use the following function:
function getGPSLocation({callBackFn}:
{callBackFn:
(v:{coords: {latitude:number; longitude:number;};}) => void
}) {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position: {coords: {latitude:number; longitude:number;};}) => {
callBackFn(position)
console.log('(U)position.coords.latitude='+JSON.stringify(position.coords.latitude))
console.log('(U)position.coords.longitude='+JSON.stringify(position.coords.longitude))
});
} else {
console.log("Geolocation is not supported by this browser.");
}
} /* End of getGPSLocation */
An error message appears in the Web Developer Tools console of my web browser:
Uncaught TypeError: t is not a function
NextJS 10
page-1aeb39b39d44f814.js:1:332
The error seems to be related to this line of code:
callBackFn(position)
If I remove that line, the expected output is displayed:
(U)position.coords.latitude=25.515932844138406 page-2bc533c4f68ab71b.js:1:339
(U)position.coords.longitude=67.32855152591916 page-2bc533c4f68ab71b.js:1:417
Why am I getting this error? And what could be the solution?
I require assistance in properly passing setCurrentGPS
(as a callback) to the getGPSLocation
function.