This interaction allows you to geocode a location. You can use this to populate forms using the user's location. The elements you can populate are:
This is a singleton widget. This means only a single geocoder can exist per page.
Some browsers won't do this locally, so use Dropbox for testing. Also, it's important to ensure that location services are enabled on the device and/or browser.
data-ur-set="reverse-geocode"
data-ur-reverse-geocode-component="rg-street"
data-ur-reverse-geocode-component="rg-city"
data-ur-reverse-geocode-component="rg-zip"
data-ur-reverse-geocode-component="rg-state"
data-ur-reverse-geocode-component="rg-country"
data-ur-reverse-geocode-component="rg-button"
data-ur-set="reverse-geocode"
data-ur-error-callback
: inline JavaScript that will be called if geolocation doesn't fully succeed
data-ur-reverse-geocode-component="rg-button"
data-ur-reverse-geocode-component="rg-street"
data-ur-reverse-geocode-component="rg-city"
data-ur-reverse-geocode-component="rg-state"
data-ur-reverse-geocode-component="rg-zip"
data-ur-reverse-geocode-component="rg-country"
You can also directly initialize the widget in JavaScript (components can be passed in as strings/elements/arrays):
Uranium.lib.geocode({ set: "#a", "rg-city": "#b", "rg-zip": "#c" });
The demonstration here activates on page load as long as location services are enabled.
<form data-ur-set="reverse-geocode" data-ur-error-callback="alert('Geolocation failed or was not allowed.')"> <input type="text" data-ur-reverse-geocode-component="rg-street"> <input type="text" data-ur-reverse-geocode-component="rg-city"> <input type="text" data-ur-reverse-geocode-component="rg-zip"> <select data-ur-reverse-geocode-component="rg-state"> <option value="">None</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> ... </select> <select data-ur-reverse-geocode-component="rg-country"> <option value="" selected>Select Country</option> <option value="United States">United States</option> <option value="United Kingdom">United Kingdom</option> ... </select> </form>
Activating the geolocator with a button
Populate text inputs rather than select elements
Does the browser always have to ask for a user to share their location?
Yes. It's a security feature of all browsers.