JS手機定位地理位置

來源:互聯網
上載者:User

標籤:

/*** 以下為html5代碼,擷取地理位置*//** * 設定地址 */function setAddress(json) {    var position = document.getElementById("txtPosition");    //    var province = json.province;        //    var city = json.city;    //    var district = json.district;    province = province.replace(‘市‘, ‘‘);     position.value = province + "," + city + "," + district;    position.style.color = ‘black‘;   }/*** 擷取地址位置成功*/function showPosition(position) {    //獲得經度緯度    var x = position.coords.latitude;    var y = position.coords.longitude;    //配置Baidu Geocoding API    var url = "http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b" +            "&callback=renderReverse" +            "&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 + "]地址位置擷取失敗,請手動選擇地址");        }    });}/*** 擷取地址位置失敗[暫不處理]*/function showError(error) {    switch (error.code) {        case error.PERMISSION_DENIED:            alert("定位失敗,使用者拒絕請求地理定位");            //x.innerHTML = "User denied the request for Geolocation.[使用者拒絕請求地理定位]"            break;        case error.POSITION_UNAVAILABLE:            alert("定位失敗,位置資訊是不可用");            //x.innerHTML = "Location information is unavailable.[位置資訊是不可用]"            break;        case error.TIMEOUT:            alert("定位失敗,請求擷取使用者位置逾時");            //x.innerHTML = "The request to get user location timed out.[請求擷取使用者位置逾時]"            break;        case error.UNKNOWN_ERROR:            alert("定位失敗,定位系統失效");            //x.innerHTML = "An unknown error occurred.[未知錯誤]"            break;    }}function getLocation() {    //檢查瀏覽器是否支援地理位置擷取    if (navigator.geolocation) {        //若支援地理位置擷取,成功調用showPosition(),失敗調用showError        //        alert("正在努力擷取位置...");        var config = { enableHighAccuracy: true, timeout: 5000, maximumAge: 30000 };        navigator.geolocation.getCurrentPosition(showPosition, showError, config);    } else {        //alert("Geolocation is not supported by this browser.");        alert("定位失敗,使用者已禁用位置擷取許可權");    }}

 

JS手機定位地理位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.