Skip to content

A utility for getting geo-location information via HTML5 and IP look-ups, geocoding, address look-ups, distance and durations, timezone information and more...

License

Notifications You must be signed in to change notification settings

bncoelho/geolocator

Repository files navigation

Geolocator v2

bower npm release license

© 2016, Onur Yıldırım (@onury) MIT License. Please see the Disclaimer and License.

Geolocator.js is a utility for getting geo-location information, geocoding, address look-ups, distance & durations, timezone information and more...

Features

  • HTML5 geolocation (by user permission) with improved accuracy.
  • Location by IP
  • Reverse Geocoding (address lookup)
  • Full address information (street, town, neighborhood, region, country, country code, postal code, etc...)
  • Fallback mechanism (from HTML5-geolocation to Geo-IP lookup)
  • NEW: Watch geographic position
  • NEW: Locate by mobile information
  • NEW: Get timezone information
  • NEW: Get distance matrix and duration information
  • NEW: Calculate distance between two geographic points
  • NEW: Various geographic conversion utilities
  • NEW: Get client IP
  • NEW: Fetched location includes country flag image (SVG) URL
  • NEW: Language support (depends on the service provider)
  • Supports Google Loader (loads Google APIs dynamically)
  • Dynamically creates Google Maps, on demand (with marker, info window, auto-adjusted zoom)
  • Non-blocking script loading (external sources are loaded on the fly without interrupting page load)
  • No library/framework dependencies (such as jQuery, etc...)
  • NEW: Universal module (CommonJS/Node/AMD..)
  • Small file size (9KB minified, gzipped)
  • Browser Support: IE 9+, Chrome, Safari, Firefox, Opera...

Breaking Changes

If you're migrating from v1.x to v2, you should consider the following changes:

  • Geolocator v2 is completely re-written from scratch; adding more features to it while keeping and improving the existing ones. So almost everything has changed, for good.
  • The most significant change is; we've got rid of separate callbacks for error and success. Now, we have Node-style, single callback for each async method; with err as the first argument. If it's a success, err will be null.
  • Most features make use of Google APIs. So you'll need a (single) Google API key to use them all. If you don't have a key, you can still use Geolocator like the previous versions, but with limited features.
  • .location property is dropped. You can always access the result, via the second argument of each async method's callback.
  • Geolocator now supports a single Geo-IP provider, Wikimedia. You can use geolcoator.setGeoIPSource() method to set a different Geo-IP source.
  • Read the API documentation for details...

Get Geolocator.js

Link or download via CDNJS.

Download full source code from GitHub releases.

Install via Bower:

bower install geolocator

Install via NPM:

npm install geolocator

If somehow you need the legacy version v1.2.9 and don't need the better. Here it is.

Usage:

Example below, tries to get user's geo-location via HTML5 Geolocation and if user rejects, it will fallback to IP based geo-location.

Inside the <head> of your HTML:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/geolocator/2.0.0/geolocator.min.js"></script>
<script type="text/javascript">

    geolocator.config({
        language: "en",
        google: {
            version: "3",
            key: "YOUR-GOOGLE-API-KEY"
        }
    });

    window.onload = function () {
        var options = {
            enableHighAccuracy: true,
            timeout: 6000,
            maximumAge: 0,
            desiredAccuracy: 30,
            fallbackToIP: true, // fallback to IP if Geolocation fails or rejected
            addressLookup: true,
            timezone: true,
            map: "map-canvas"
        };
        geolocator.locate(options, function (err, location) {
            if (err) return console.log(err);
            console.log(location);
        });
    };

</script>

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.

Important Notes

  • Since Geolocation API is an HTML5 feature, make sure your doctype is HTML5 (e.g. <!DOCTYPE html>).
  • Make sure you're calling Geolocation APIs (such as geolocator.locate() and 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.
  • Although some calls might work without a key, it is generally required by most Google APIs (such as Time Zone API). To get a free (or premium) key, click here. After getting a key, you can enable multiple APIs for it. Make sure you enable all the APIs supported by Geolocator.
  • On Firefox, callback is not fired for Geolocation, if user clicks "Not Now" instead of "Never". (bug 675533).

Under the Hood

Geolocator v2 is written in ES2015 (ES6), compiled with Babel, bundled with Webpack and documented with Docma.

License

MIT. See the Disclaimer and License.

About

A utility for getting geo-location information via HTML5 and IP look-ups, geocoding, address look-ups, distance and durations, timezone information and more...

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 87.1%
  • HTML 12.0%
  • CSS 0.9%