Objective
The geolocation API (geo-Location Application interface) provides a way to know the current location of a browser user. And now it seems that the browser support is good (because the new version of IE supports the API), which makes it possible to use this browser built-in API in the near future. HTML5 geolocation is just an API for retrieving location information, and as for how the underlying is positioned he doesn't know, he's the equivalent of a messenger.
1. Detection of browser support. |
In HTML5, the browser compatibility is judged by the new Geolocation property under the Window.navigator object.
function Geolocationsupport () { if (!navigator.geolocation) { alert (' current browser does not support HTML5 geolocation '); } Else { alert (' current browser supports HTML5 geolocation '); } } Geolocationsupport ();
2. Get the current location |
We use the GetCurrentPosition (success (position), error (ERR), options) method to get the current user's geographic location.
The success (position) callback function is called when obtaining geographic information, where the position parameter is an object that includes: Latitude (latitude), longitude (longitude)
Accuracy (accuracy of latitude and longitude, in meters)
Latitudeaccuracy (altitude accuracy, in meters)
Heading (The forward direction of the device), speed (the advance of the device in units m/s)
Timestamp (time to get the location).
The error callback function is called when obtaining a geo-location failure, where the Err parameter has two properties:
Code and Message,code{1: Indicates that the user has denied location services, 2: Get no position information, 3: Get information Time-out error},message is a string that represents an error message.
Opations is a list of optional properties including: Enablehighaccuracy (whether high-precision geo-location information is required), and a value of true or false.
Timeout (a time-out limit for geographic information acquisition, or an error if the timeout is exceeded), with a value of number in milliseconds.
Maximumage (a limit on the effective time to cache geo-location information), the value is a number, in milliseconds.
Window.onload = function () { var x, y; if (navigator.geolocation) { document.getElementById ("IP"). InnerHTML = "Your current location"; Baidu Map API function var map = new Bmap.map ("container"); var point = new Bmap.point (x, y); Map.centerandzoom (point,12); var geolocation = new Bmap.geolocation (); Geolocation.getcurrentposition (function (r) { if (this.getstatus () = = bmap_status_success) { var mk = new Bmap.marker (r.point); Map.addoverlay (MK); Map.panto (R.point); } else { alert (' Failed ' +this.getstatus ()); } },{enablehighaccuracy:true}) return; } };
Introduce JS:
<type= "Text/javascript" src= "http://api.map.baidu.com/api?v=2.0 &AK=134DB1B9CF1F1F2B4427210932B34DCB "></script>
3. Clear location monitoring and clear monitoring |
Watchposition with the above getcurrentposition is the same principle, parameter, return value. It's just a timer version.
Clearwatch is to clear the watchposition.
Editor
@ only Yun-hee learn from Iwebkit
Html-geolocation API