JS Mobile Positioning location

Source: Internet
Author: User

/** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.