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 recentgeolocator.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 messageString
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 positiveInteger
value will indicate the index of the source
ip-geo service (if the value is in range). Booleantrue
will set the default ip-geo service index
which is0
(Google).
Valid values:0
ortrue
(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 recentgeolocator.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 messageString
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
}