Finding the user location with HTML5 GeoLocation API is quite simple. I have been experimenting Geo api, just sharing what I learned. Follow the reference given below for more understanding.
Geolocation can be obtained from navigator object like navigator.geolocation.
First step check whether your browser supports geolocation. If not fall back to native javascript.
if(navigator.geolocation)
else alert("your browser doesn't support geolocation")
geolocation object has 3 methods
getCurrentPosition & watchPosition looks similar and takes same arguments.
ex:
navigator.geolocation.getCurrentPosition(success, error, {map of geolocation options});
success = function(position) {
position.coords.latitude ;
position.coords.longitude ;
position.coords.accuracy;
};
failure = function(error) {
// 3 types of error
1: 'Permission denied',
2: 'Position is not available',
3: 'Request timeout'
};
Failure return error code of 1,2,3.
The third argument for both getCurrentPosition and watchPosition is the geolocation options which is optional.
1) enableHightAccuracy - takes boolean true/false, enables the device to get more accurate latitude & longitude positions
2) timeout - time allowed for the device to get the data. keep it zero will let the browser to never timeout (in milliseconds). Setting to 2000 will wait for 2000 milliseconds before it quits with REQUEST TIME OUT error code 3.
3) maximumAge - lets the browser to use the cached data. if set to zero will always look up new position for each request.
I have made a small demo which is still in work in progress.
Reference:
Geolocation API Specification
HTML5 By Bruce Lawson and Remy Sharp
Geolocation can be obtained from navigator object like navigator.geolocation.
First step check whether your browser supports geolocation. If not fall back to native javascript.
if(navigator.geolocation)
else alert("your browser doesn't support geolocation")
geolocation object has 3 methods
- getCurrentPosition
- watchPosition
- clearWatch
getCurrentPosition & watchPosition looks similar and takes same arguments.
ex:
navigator.geolocation.getCurrentPosition(success, error, {map of geolocation options});
success = function(position) {
position.coords.latitude ;
position.coords.longitude ;
position.coords.accuracy;
};
failure = function(error) {
// 3 types of error
1: 'Permission denied',
2: 'Position is not available',
3: 'Request timeout'
};
Failure return error code of 1,2,3.
The third argument for both getCurrentPosition and watchPosition is the geolocation options which is optional.
1) enableHightAccuracy - takes boolean true/false, enables the device to get more accurate latitude & longitude positions
2) timeout - time allowed for the device to get the data. keep it zero will let the browser to never timeout (in milliseconds). Setting to 2000 will wait for 2000 milliseconds before it quits with REQUEST TIME OUT error code 3.
3) maximumAge - lets the browser to use the cached data. if set to zero will always look up new position for each request.
I have made a small demo which is still in work in progress.
Reference:
Geolocation API Specification
HTML5 By Bruce Lawson and Remy Sharp
2 comments:
Is there an API available to get geo location by using IP address.. I know off an api provided by infochimps.com , but there is a minimal charge..
HTML5 GeoLocation API does not support IP address.
Google's ClientLocation uses IP address geolocation.
google.loader.ClientLocation
I haven't tried it yet.
There are couple of external geolocation service. These services use geolocation databases to map the IP address of a device to geographic locations.
https://developers.google.com/loader.
Post a Comment