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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Geolocator Javascript Lib v.0.92

Author: Onur YILDIRIM (www.cutepilot.com) © 2012. MIT License.

###Features:

  • HTML5 geolocation (by user permission)
  • Location by IP (Supported source services: Google, freegeoip, geoplugin, wikimedia)
  • Reverse Geocoding (address lookup)
  • Full address information (street, town, neighborhood, region, country, country code, postal code, etc...)
  • Fallback mechanism (from HTML5-geolocation to IP-geo lookup)
  • Supports Google Loader (loads Google Maps dynamically)
  • Dynamically creates Google Maps (with marker, info window, auto-adjusted zoom)
  • Non-blocking script loading (external sources are loaded on the fly without interrupting page load)

###Usage:

Inside the <head> of your HTML:

<script type="text/javascript" src="geolocator.js"></script>
<script type="text/javascript">
    //The callback function executed when the location is fetched successfully.
    function onGeoSuccess(location) {
        console.log(location);
    }
    //The callback function executed when the location could not be fetched.
    function onGeoError(message) {
        console.log(message);
    }

    window.onload = function() { 
        //geolocator.locateByIP(onGeoSuccess, onGeoError, 2, 'map_canvas');
        var html5Options = { enableHighAccuracy: true, timeout: 3000, maximumAge: 0 };
        geolocator.locate(onGeoSuccess, onGeoError, true, html5Options, 'map_canvas');
    }
</script>

Also place the line below, inside your <body> if you want to dynamically draw a map (you should also pass the element ID to the corresponding method for the map to be drawn):

<div id="map_canvas" style="width:600px;height:400px"></div>

geolocator.js provides 2 useful methods:

##Methods

###geolocator.locate()
Use this method to get the location via HTML5 geolocation.

geolocator.locate( successCallback, [errorCallback], [fallbackToIP], [html5Options], [mapCanvasId] )

Parameters:

successCallback Function
  A callback function to be executed when the location is successfully fetched.
  The recent geolocator.location object is passed to this callback, as the only argument.

errorCallback Function (optional, default: null)
  A callback function to be executed when the location could not be fetched due to an error.
  The recent error message String is passed to this callback, as the only argument.

fallbackToIP Boolean|Integer (optional, default: false)
  Specifies whether geolocator should fallback to IP geo-lookup when HTML5 geolocation is not
  supported or rejected by the user. A positive Integer value will indicate the index of the source
  ip-geo service (if the value is in range). Boolean true will set the default ip-geo service index
  which is 0 (Google).
  Valid values: 0 or true (use Google for ip-geo fallback), 1 (use FreeGeoIP for ip-geo fallback),
  2 (use GeoPlugin for ip-geo fallback), 3 (use Wikimedia for ip-geo fallback), false or -1 or
  null or any other value (will disable ip-geo fallback)

html5Options Object (optional, default: null)
  HTML5 geolocation options.

mapCanvasId String (optional, default: null)
  HTML element ID for the Google Maps canvas. If set to null, no map is drawn.

Example:

var html5Options = { enableHighAccuracy: true, timeout: 3000, maximumAge: 0 };
geolocator.locate(onGeoSuccess, onGeoError, true, html5Options, 'map_canvas');

###geolocator.locateByIP()
Use this method to get the location from the user's IP.

geolocator.locateByIP( successCallback, [errorCallback], [ipSourceIndex], [mapCanvasId] )

Parameters:

successCallback Function
  A callback function to be executed when the location is successfully fetched.
  The recent geolocator.location object is passed to this callback, as the only argument.

errorCallback Function (optional, default: null)
  A callback function to be executed when the location could not be fetched due to an error.
  The recent error message String is passed to this callback, as the only argument.

ipGeoSourceIndex Integer (optional, default: 0)
  Indicates the index of the IP geo-lookup service.
  Valid values: 0 (Google), 1 (FreeGeoIP), 2 (GeoPlugin), 3 (Wikimedia)

mapCanvasId String (optional, default: null)
  HTML element ID for the Google Maps canvas. If set to null, no map is drawn.

Example:

geolocator.locateByIP(onGeoSuccess, onGeoError, 0, 'map_canvas');

##Properties

###geolocator.location Object
Provides the recent geo-location information.

Example Output:

{
    address: {
        city: "New York",
        country: "United States",
        countryCode: "US",
        neighborhood: "Williamsburg",
        postalCode: "11211",
        region: "New York",
        street: "Bedford Avenue",
        street_number: "285",
        town: "Brooklyn"
        },
    coords: {
        accuracy: 65,
        altitude: null,
        altitudeAccuracy: null,
        heading: null,
        latitude: 40.714224,
        longitude: -73.961452,
        speed: null
        },
    map: {
        canvas: HTMLDivElement, //DOM element for the map
        map: Object, //google.maps.Map
        options: Object, //map options
        infoWindow: Object, //google.maps.InfoWindow
        marker: Object //google.maps.Marker
        },
    formattedAddress: "285 Bedford Avenue, Brooklyn, NY 11211, USA",
    ipGeoSource: null,
    timestamp: 1360582737790
}

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%