/** * The following is the HTML5 code, get the location*//** * Set address*/functionsetaddress (JSON) {varPosition = document.getElementById ("Txtposition"); //Province varProvince =json.province; //City varCity =json.city; //Area vardistrict =json.district; Province= Province.replace (' City ', ' '); Position.value= Province + "," + City + "," +District; Position.style.color= ' Black '; }/** * Get address location successful*/functionshowposition (position) {//Get longitude Latitude varx =Position.coords.latitude; vary =Position.coords.longitude; //Configuring the Baidu Geocoding API varurl = "http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b" + "&callback=renderrevers E "+" &location= "+ x +", "+ y +" &output=json "+" &pois=0 "; $.ajax ({type:"GET", DataType:"Jsonp", Url:url, success:function(JSON) {if(JSON = =NULL||typeof(JSON) = = "Undefined") { return; } if(Json.status! = "0") { return; } setaddress (Json.result.addressComponent); }, Error:function(XMLHttpRequest, Textstatus, Errorthrown) {alert ("[x:" + x + ", y:" + y + "] address location acquisition failed, please manually select Address"); } });}/** * Get address location failed [pending]*/functionShowError (Error) {Switch(error.code) { Caseerror. Permission_denied:alert ("Location failed, user refused to request geo-location"); //x.innerhtml = "User denied the request for geolocation. [user refused to request geo-location] " Break; Caseerror. Position_unavailable:alert ("Location failed, position information is not available"); //x.innerhtml = "Location information is unavailable. [Location information is not available] " Break; Caseerror. Timeout:alert ("Location failed, request get user location timed out"); //x.innerhtml = "The request to get a user location timed out. [Request get user Location timeout] " Break; Caseerror. Unknown_error:alert ("Location failure, location system failure"); //x.innerhtml = "An unknown error occurred. [Unknown ERROR] " Break; }}functiongetLocation () {//check whether the browser supports geo-access if(navigator.geolocation) {//if geo-location is supported, successful call to Showposition (), failed call ShowError //alert ("Trying to get position ..."); varConfig = {enablehighaccuracy:true, timeout:5000, maximumage:30000 }; Navigator.geolocation.getCurrentPosition (showposition, showerror, config); } Else { //alert ("Geolocation is not supported by this browser.");Alert ("Failed to locate, user has disabled location get permission"); }}
JS Mobile Positioning location