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.
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.
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…?
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.
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
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 :).