So fist of im using angular and wanted to download current geological coordinates and send them to my api.When i run this code it shows error " Cannot read properties of null (reading 'getData') ".Im sure that the getData function is working becouse when i refer to it somewhere else for example : this.getData(a,b) (a and b is given by a user) everyfing goes fine.Here is my code:
showPosition(position:any) {
let g = position.coords.latitude;
let f = position.coords.longitude;
this.getData(g,f);
};
getLocation() {
navigator.geolocation.getCurrentPosition(this.showPosition);
};
getData(lat:any , lng:any) {
this.httpClient.get(`https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lng}&hourly=temperature_2m,relativehumidity_2m,windspeed_10m`)
.subscribe((res:any) => {
this.weatherData = res;
this.findTodayDate();
});
};
I tried moving the whole getData func to show position but then i had problem with httpClient. i tried to give static numer to the show position like
showPosition(position:any) {
this.getData(23,23);
};
but got the same error i think that problem is with the way that navigator.geolocation.getCurrentPosition(this.showPosition); uses funcions.
Use navigator.geolocation.getCurrentPosition instead:
showPosition() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position.coords.latitude, position.coords.longitude)
let g = position.coords.latitude;
let f = position.coords.longitude;
this.getData(g, f);
}
);
}
};