Hello, Everybody!
I am working on the “Show the Local Weather” project but I got stuck at the very beginning as it seems my getCurrentPosition() does not trigger the callback function. Here is my code (view on codepen):
if(navigator.geolocation) {
console.log("navigator.geolocation is available");
navigator.geolocation.getCurrentPosition(function(position) {
console.log("current position acquired");
});
}
When I run it, in the console I can see the first message (“navigator.geolocation is available”) but not the second (“current position acquired”). I have already checked that my browser settings allow access to my location, therefore I have no idea about what the problem could be; any suggestion?
function getCurrentPos() {
console.log(“current position acquired”);
X= navigator.geolocation.getCurrentPosition;
console.log(“current position acquired”);
};
getCurrentPos();
}
1.I think that if you would define the function before it will make your code more organized.
2. The function navigator.geolocation.getCurrentPosition is already defined,
here is a great example from W3C of using geolocation:
var x = document.getElementById(“demo”);
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = “Geolocation is not supported by this browser.”;
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
" Longitude: " + position.coords.longitude;
}
I tried Google Chrome and Mozilla Firefox but I didn’t even considered trying MSIE as usually, if something doesn’t work on Chrome and Firefox, is not going to work on MSIE too… but actually you’re right, everything works as expected on MSIE, thank you very much!
Do you have any idea about how to solve or work-around this browser support issue?
I’m on mobile so Im having trouble finding it, but I found an API that returns state + country, and the weather API I wa using could take geolocation automatically from the browser. I switched to this after running into the same HTTPS issue.
Have you thought about using a free IP geolocation API? The benefit is the user is not required to approve the geolocation permission request. So the weather condition will display immediately. You can look up the documentation here: freegeoip.net
Actually I haven’t thought about using a different API, as the assignment suggested the OpenWeatherMap one, but I will definitely have a look at it; thank you very much for the hint.
It’s not a replacement API for OpenWeather. You can use both, getting the longitude and latitude from the IP Geolocation API and passing it to the OpenWeather API. Personally I think the IP Geolocation API is a lot easier to implement than the navigator.geolocation used within the browser. I saw the example code for it on Mozilla Developer Network and its quite long. I did implement both but ended up using the IP Geolocation for simplicity to the user and my self.
navigator.geolocation.getCurrentPosition(success, error);
function success(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var GEOCODING = ‘https://maps.googleapis.com/maps/api/geocode/json?latlng=’ + lat + ‘%2C’ + lon + ‘&language=en’;