PRO: User Location

How to get the weather based on the location of your user.

The User Location API uses the IP address of the visiting user to attempt to locate them to the closest city found in the weather providers database.

Because there are over 4 million IPs and they are continually being reassigned to different places, the IP lookup service is not 100% accurate and never will be.

If a suitable IP location is not located it will fall back to your default location. My plugin uses ipinfo.io to locate the user. You can visit that page to see where the service thinks you are.

Users are able to use HTML5 Geolocation which is based off latitude and longitude and more accurate.

The example widget on the purchase page is using this functionality. Please test this widget to determine if my plugin will work in your location and for your needs.

Sequence of ‘Use User Location’

The plugin goes through a few checks once activated:

  1. On the first visit of your user, the plugin will attempt to use the IP address to find the city.
  2. If a city cannot be found it will prompt the user to set their own city:

set-your-weather

The reason this workflow was developed is designed around the user experience.

Getting the city based off the IP is a good first attempt because the user doesn’t have to do anything (and is generally pretty close).

The HTML5 Geolocation method opens a popup and asks the user for ‘Access to their location’ when they first hit your site. This may be confusing to the user, especially if your weather widget is not immediately visible on the page (in the footer).

You can always build a custom template with a different workflow.

Setup

To turn on the user location features, you can do so in the widget settings by clicking the ‘Use User Location’ checkbox.

use-user-locaiton

You can also pass an attribute when using a shortcode to setup your widgetuse_user_location="1"

Starting in version 1.1 there are two new options that should appear when you check the checkbox in the widget settings. You can now select to auto grab the C or F and also allow the user to change their weather.

1-1-new-settings

If you choose the ‘Allow User to Change the Location’ setting, (attribute allow_user_to_change="1" in the shortcode) your widget should show a small map marker in the corner of your widget.

weather-widget-map-marker

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

know-your-location

If the user allows the request, your widget will populate (via AJAX, no page reload) with the new city data. If the user blocks the request they will be presented with a search box to choose their own city.

user-location-fallback