Problem with geolocation

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?


Hi, I’m new to js also but I made this changes:

function getCurrentPos() {
console.log(“current position acquired”);
X= navigator.geolocation.getCurrentPosition;
console.log(“current position acquired”);


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) {
} else {
x.innerHTML = “Geolocation is not supported by this browser.”;
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
Longitude: " + position.coords.longitude;

good luck!

getCurrentPosition() is a method of the geolocation object; it doesn’t need to be declared upfront.

What browser are you using? Chrome?

Try using this in your browser and see what result you get.

I tried it on my Chrome browser and got an error, but when I tired it on IE i got the location ok which is ironic.

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 believe it might be something to do with this.

Will have a bit more of a look into it tonight when I get back from work :smiley:

Thank you very much again for your support :slight_smile:

No worries :smile: Let me know if you find a solution, will have a look at this tonight too.

For what concerns Firefox, updating it to the latest version was enough to solve the issue. I’ll let you know if I find a solution fro Chrome too.

If you’re using Chrome you might have to type ‘https://’, that worked for me in Chrome and firefox asked if I wanted to share the location.

Other problem with this task was getting the data back from openweather api. The data is viewable in the browser but I’m unable to retrieve the data.

1 Like

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:

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. :wink:

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.

Run it over https. Like https://codepen.ip/yourname/yourpen and it will work. it should end up like this

navigator.geolocation.getCurrentPosition(success, error);
function success(position) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
var GEOCODING = ‘’ + lat + ‘%2C’ + lon + ‘&language=en’;

Hey a lot of people have had this problem. Check out this forum
And take a look at my project to see a working example.

the geolocation feature in the browser giving you the location of the ISP not your current location , it is not accuired for me