Polymer element for client geolocation lookup with freegeoip.net
bower install --save polymer-freegeoip-lookup<link rel="import" href="freegeoip-lookup.html">
<freegeoip-lookup></freegeoip-lookup>
<script type="text/javascript">
let el = document.querySelector('freegeoip-lookup');
el.addEventListener('response-changed', (response) => {
if(el.error) {
alert('There was an error getting your location. Check your Ad-Blocker.');
} else {
alert('Freegeoip.net thinks you are at ' + el.response.latitude + ',' + el.response.longitude);
}
});
</script>The component can be configured via attributes on the HTMLElement.
| Option | Default | Description |
|---|---|---|
| url | https://freegeoip.net/json |
lookup url, self-host freegeoip |
| disable-autoload | false |
does not automatically fire lookup on-load |
By calling the element's lookup() function a new request is sent to the IP lookup API. Changing the url attribute at runtime does not automatically trigger a new request.
| Attribute | Description |
|---|---|
| response | An Object containing the server response. { latitude: 12, longitude: 34 } |
| error | Boolean value that is true, if there was an error requesting the location |
| loading | Boolean value that is true during active lookups |
- response-changed
- loading-changed
- error-changed
document.querySelector('freegeoip-lookup').addEventListener('response-changed', doSomething);