Getting coordinates from navigator.geolocation.getCurrentPosition works sporadically. Please Help

Hello,

I’ve put together the demo for the ‘Local Weather’ and it was working fine yesterday and then today the navigator.geolocation.getCurrentPosition() function is consistently either timing-out or returning Error Code 2.

I’ve tried it on several browsers and several devices and I can’t figure out what’s going wrong.

I’ve put in some demo data for testing purposes, so Yelandur, IN is the default location but for some reason the actual coordinates are not loading. Once that happens all the API stuff to get the weather and display it is perfect. :confused:

Thanks in advance, please let me know if there’s any other information that’s missing.

Most of the modern borwser vendors have recently introduced some restrictions to the geolocation functionality, due to privacy reasons. For exaple, it doesnt work in chrome if a site asking for a coordinates is considered insecure.
Honestly, it’s been such a pain in the ass to work around this, that i actually used this for geolocation.

2 Likes

I’m going to be going back and redoing my Weather project, and I’m definitely going to use that, thanks!

Its not a pain in the ass if you use a different weather API that supports https requests for free. Apixu, or dark sky for instance.

That way you can use Geo with HTTPS in the browser address, avoid cross origin issues and avoid third party library complicating things.

The IP geo location is terribly inaccurate @JavaTheNutt . It thinks you live at wherever your internet connection terminates. Thats 200 miles away for me some days, and about 40 miles away on others. The link i just clicked thinks i’m in Milton Keynes, which is 3 or 4 hours drive away from here.

Keep in mind tho, that this api isn’t very accurate. Right now it shows me coordinates of a place about 20km away from where I sit. It might be good enought for weather, but its definetely insuficient for other purposes, like using it in conjuction with google maps.

@MARKJ78 I thought the main problem is codepen not being secure…?

1 Like

I loaded codepen via https:// as well and it didn’t seem to make too much of a difference so I’m not sure.

Not Codepen as such, its having a secure connection (ie https) that matters.

A new standard coming into effect will stop Geolocation requests on insecure website connections (http). Google have just implemented it early in Chrome.

The trouble is when you switch to HTTPS on codepen, the geo works but that pesky OpenWeather API doesn’t because it’s http. So you end up with that cross origin error and no weather object. You have to either pay open weather for a upgraded account to get https or just switch to another API that supports it for free.

You can have your cake and eat it :slight_smile:

1 Like

Hi @jLouzado

If you haven’t already, now might be a good time to get into using dev tools in your browser.

Right click on your page, inspect element, console tab.

If you look in there you will see there are errors that are halting your script - and thats your starting point :slight_smile:

Thanks, that clarifies a lot!

1 Like

Hmm, I guess it’s back to the drawing board so @MARKJ78. I just checked the coordinates there, and it thinks I’m right in Dublin City Center, when I’m actually about 20 miles outside of Dublin. Would you recommend Dark Sky or APIXU? I suppose it’s all rather moot anyway, since I wont be doing the project in CodePen, and I’m not going to fork out for an SSL cert on Heroku

@JavaTheNutt I used Apixu with no problems at all but I haven’t tried dark skies so I can’t compare

1 Like

You could always use github pages. With a non custom domain you’re forced onto HTTPS anyway there.

1 Like

That’s perfect, I didn’t know that about GitHub pages. Thanks again!

Ah GH pages… its so easy to use too. Great for front end stuff that can be public.

Simply go into the settings on your repo and enable it on the master branch :thumbsup:

I was indeed using dev tools to try and figure out what was happening. On Firefox 50.1.0 the error is either that unknown error while getting location (paraphrased) or that position acquisition timed out.

I’ll switch to use Apixu or some other API, do you mean that these give GEO-location as well?

No you get the geo from the browser like you currently are, check it in the logs to see what your getting. Then build your request URL with that, like you have already :).

If you don’t mind, I’ll just check your weather app in your profile and see if you’ve done anything differently because I seem strangely stuck here.

If I can’t figure it out I’ll come back and ask you guys for help. Thanks so much. :slight_smile:

1 Like

I went to town on mine @jLouzado !

Lines 60 to 160