Geolocator.js is a utility for getting geo-location information, geocoding, address look-ups, distance & durations, timezone information and more...
bower install geolocator
npm install geolocator
Example below, will attempt to get user's geo-location via HTML5 Geolocation and if user rejects, it will fallback to IP based geo-location.
<head> of your HTML:
If you've enabled
map option; include the following, inside the
<body> of your HTML:
<div id="map-canvas" style="width:600px;height:400px"></div>
Read API documentation for lots of other features and examples.
doctypeis HTML5 (e.g.
geolocator.watch()) from a secure origin (i.e. an HTTPS page). In Chrome 50+, Geolocation API is removed from unsecured origins. Other browsers are expected to follow.
geolcoator.setGeoIPSource()method to set a different Geo-IP source, but see the Caveats section before you do so.
Mixed Content Restriction:
There are alternative Geo-IP services to be used with this library. But most of these services do not provide a free API over HTTPS (SSL/TLS). You need to subscribe for a premium API key to use HTTPS. The caveat is; HTML5 Geolocation API is restricted to HTTPS and when you enable the
fallbackToIP option, some browsers (such as Chrome and Firefox) will not allow mixed content. It will block HTTP content when the page is served over HTTPS.
Currently, Geolocator will use GeoJS by default which is free and supports HTTPS. Please use this service responsibly. For example, do NOT call
.locatebyIP() on every user/request but only on site entrance. Their location or IP will not change suddenly unless they can teleport!..
Also, support GeoJS if you can so we can continue using this nice free service. If you know other free Geo-IP services over HTTPS, let me know and we can add/use them as alternatives.
Isomorphic Applications / SSR Support:
This library currently only works on client-side (browser). Any kind of server-side functionality (including Server Side Rendering) is not yet oficially supported.
There is a discussion about at least preventing it from throwing on server; or maybe adding support for some (if not all) methods to work on server. If you're interested and willing to contribute;
See version changes here.