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

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

#1

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.


#2

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.


#3

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


#4

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.


#5

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…?


#6

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


#7

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:


#8

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:


#9

Thanks, that clarifies a lot!


#10

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


#11

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


#12

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


#13

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


#14

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:


#15

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?


#16

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


#17

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:


#18

I went to town on mine @jLouzado !

Lines 60 to 160