HTML5 geolocation API (geolocation application interface)
Current PC Browser support situation:
Firefox 3.5+
Chrome 5.0+
Safari 5.0+
Opera 10.60+
Internet Explorer 9.0+
Phone Support situation:
Android 2.0+
IPhone 3.0+
Opera Mobile 10.1+
Symbian (S60 3rd & 5th Generation)
Blackberry OS 6
Maemo
To detect whether the browser supports:
If (navigator.geolocation) { //Console.log ("browser support!"); }Else// Console.log ("Browser not supported!");}
The navigator.geolocation is used to obtain a browser-based current user location and provides 3 methods:
void getcurrentposition (onsuccess,onerror,options); gets the user's current position int watchcurrentposition (onsuccess,onerror,options); continuous acquisition of the current user location void Clearwatch (watchid); Watchid value returned for watchcurrentposition // de-monitoring
Onsuccess method is called when successful (required), OnError method failure is called (optional), options other parameters (optional)
Options
Options = { enablehighaccuracy, //Boolean requires high-precision geo-information timeout, //Indicates the maximum time to wait for a response , the default is 0 milliseconds That represents the infinite time maximumage /Application cache time }
The position object is returned in the Onsuccess method, where information about the location can be obtained
<!DOCTYPE html><Html><Head><MetaCharSet= "Utf-8"/><Title> Browser-based HTML5 find a location</Title><!--Baidu API-<ScriptSrc= "http://api.map.baidu.com/api?v=1.2"Type= "Text/javascript"></Script><Script>functionGetLocation () {VarOptions={enablehighaccuracy:True, Maximumage:1000}If(navigator.geolocation) {//Browser support geolocationNavigator.geolocation.getCurrentPosition (onsuccess,onerror,options); }Else{//Browser does not support geolocation} }//When successfulfunctionOnsuccess (position) {//Return to User location//LongitudeVarLongitude=Position.coords.longitude;//LatitudeVarLatitude=Position.coords.latitude;//Use Baidu Map API//Create a Map instanceVarMap=NewBmap.map ("Container");//Create a coordinateVarPoint=NewBmap.point (Longitude,latitude);//Map initialization, setting center point coordinates and map levelsMap.centerandzoom (Point,15); }//When failedfunctionOnError (Error) {Switch(Error.code) {Case1: Alert ("Location service is denied");Break;Case2: Alert ("Temporarily not getting location information");Break;Case3: Alert ("Get information timeout");Break;Case4: Alert ("Unknown error");Break; }} window.onload=GetLocation;</script></head><body< Span style= "color: #0000ff;" >> <div id= "Container" Style= "width:600px;height:600px" ></div> </body></< Span style= "color: #800000;" >html>
Getting the current position plus the offset will be a bit
Longitude + Longitude calibration value: 0.008774687519;
Latitude + Longitude correction value: 0.00374531687912;
HTML5 Location Locator API (2)