Geolocation and Google MAP API Quick Guid
Geolocation API
Geolocation API is used by a Web browser to get the location information of the user, which is critical in the development of a location-aware Web application. Geolocation object is a property of navigator object. It supports the following three method calls:
- void getCurrentPosition(successCallback, [ errorCallback, options ]): it returns immediately. When the location information becomes available, successCallback( ) function will be called with a position object argument; otherwise, errorCallback( ) function will be called with a positionError object argument. Optional argument options can be used to specify the behavior and options;
- long watchPosition(successCallback, [ errorCallback, options ]): it is similar to getCurrentPosition( ), but it returns a watch ID because it monitors changes in location. Every time there is a location change, successCallback( ) function will be called. It is meaningless to a Desktop computer;
- void clearWatch( watchID ): it cancels the watch operation. watchPosition( )/clearWatch( ) work like setInterval( )/clearInterval( ).
position object includes the following properties:
- coords:
- latitude, longitude: geographic coordinates in decimal degrees;
- altitude: the height in meters. It could be null;
- accuracy, altitudeAccuracy: accuracy levels in meters. altitudeAccuracy could be null;
- heading: direction of movement in range of 0deg and 360deg. North is 0deg, and it counts clockwise;
- speed: horizontal speed in units of meters per second. it could be null.
- timestamp: the number of milliseconds;
positionError object includes the following properties:
- code: error code. It could be PERMISSION_DENIED(1), POSITION_UNAVAILABLE(2), TIMEOUT(3);
- message;
positionOptions object argument for getCurrentPosition()/watchPosition() includes the following properties:
- enableHighAccuracy: a boolean value to give a hint if the best possible results is desired. It may lead to slow response or high power consumption;
- timeout: the number of milliseconds to wait for the position information to become available. If it times out, errorCallback() function will be called with timeout error;
- maximumAge: the freshness of the cached position information that is acceptable, in the units of milliseconds. If it is 0, a new position will be queried.
An example is given below to see how to use Geolocation API:
navigator.geolocation.getCurrentPosition(function(position) {
// do whatever with position.coords.longtitude and position.coords.latitude
...
}, function(error) {
// do whatever with error object
...
});
Google Maps API Guide
To include a Google MAP in the Webpage, follow the following steps:
- Obtain an API key at the APIs Console with a Google account;
- Include Google Maps API in the Webpage: <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE"> </script>. Replace YOUR_API_KEY with your API key, and SET_TO_TRUE_OR_FALSE to true or false on using a sensor (like a GPS chip);
- Include an empty div section in the Webpage with size spcified. Suppsoe its id is "map";
- Specify the map's options in a MapOptions object, such as the center coodinates of the map, zoom level, map type, etc.;
- Load the map: var map = new google.maps.Map(document.getElementById("map"), mapOptions);
References
- Geolocation API Specification
- Using geolocation
- Google Maps JavaScript API v3
- Anthony T. Holdener III, HTML5 Geolocation