HTML5: geolocation
With HTML5 is now possible to use the target browser in order to determine the location from which the user is accessing the web. It is not, as you may understand, a GPS location (unless the user is surfing from a GPS capable device), but it can be quite interesting in some cases where special services are supplied on a web page.
How can we develop a geolocation service on our web page?
Well, first of all try and see the final effect: go here and enjoy. After that, please get back on the web thought...
First of all let me say that all location services on the target browser must be granted by the user. As you may have noticed by visiting the above live example, the browser has asked you permission for the use of location services (unless you've granted permanent access to your location).
But how is it working?
Well, this time is not just a line of code. In fact, we need to use a little snippet. Everything's working around the geolocation API.
Let's see a complete example:
1) the two divs at the beginning of the above snippet are used to show possible error (errMsg) and the resulting map (map);
2) the getLoc() function is used to determine the geolocation;
3) the showPos() function is actually showing the map by inserting it in the div with id="map";
4) the showErr() function is replacing the innerHTML of the div with id="errMsg" with a possible error message (something went wrong).
If you spend a little time on studying the above script, everything will be quite clear.
What about compatibility? Well, all the latest version of all the available browser are supporting geolocation. Good news eh?
If you like, you can share your thoughts by using the comments section below. If you don't want... well.. don't.
How can we develop a geolocation service on our web page?
Well, first of all try and see the final effect: go here and enjoy. After that, please get back on the web thought...
First of all let me say that all location services on the target browser must be granted by the user. As you may have noticed by visiting the above live example, the browser has asked you permission for the use of location services (unless you've granted permanent access to your location).
But how is it working?
Well, this time is not just a line of code. In fact, we need to use a little snippet. Everything's working around the geolocation API.
Let's see a complete example:
The script itself is quite easy to understand, but I will here explain some important things:1) the two divs at the beginning of the above snippet are used to show possible error (errMsg) and the resulting map (map);
2) the getLoc() function is used to determine the geolocation;
3) the showPos() function is actually showing the map by inserting it in the div with id="map";
4) the showErr() function is replacing the innerHTML of the div with id="errMsg" with a possible error message (something went wrong).
If you spend a little time on studying the above script, everything will be quite clear.
What about compatibility? Well, all the latest version of all the available browser are supporting geolocation. Good news eh?
If you like, you can share your thoughts by using the comments section below. If you don't want... well.. don't.