Detecting the User’s Location

Here is the workflow of the User Location feature in the Awesome Weather Widget for WordPress.

Check #1 – Is the setting on?

If the widget is being displayed through the shortcode we look for the attribute use_user_location="1". If the widget is set up from the Widgets section of the admin we look for this checkbox to be checked.

Check #2 – Do we have an IP Address lookup service?

The first element we use to detect the location of the user is the IP address of the user’s computer or device. The service we use to do that is Their free account provides 1,000 lookups per day.

Note: Because there are millions of IP addresses and they are continually being reassigned to different places, the IP lookup service is not 100% accurate.

In your WordPress admin under 'Settings' -> 'Awesome Weather', make sure that you have added your Token or this check will not take place.

Check #3 – Is a default location set, just in case?

If, for whatever reason, the IP of the user cannot be found the widget will default to the location specified in one of the following attributes: latlng, owm_city_id or location. If you are using widget settings, it will be the ‘Search for your Location’ field or OpenWeatherMap City ID (if not using Dark Sky as your weather provider).

If a default location is not set and the IP Address doesn’t find the user, the widget will return blank!

Check #4 – Do you want to use HTML5 Geolocation?

This feature attempts to use the browser to get the latitude and longitude of the device or computer your user is using. This is only supported on websites that use HTTPS. The user can then hit the pin icon in the corner to set their weather.

When clicked it will prompt the user to allow your website access to their location.

If the user allows the request, your widget will populate with the new weather data. If the user blocks the request they will be presented with a search box to choose their own city.