zoukankan      html  css  js  c++  java
  • JS手机定位地理位置

    /**
    * 以下为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=KEY" +
                "&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("定位失败,用户已禁用位置获取权限");
        }
    }
  • 相关阅读:
    各类免费资料及书籍索引大全(珍藏版)
    转—如何写一篇好的技术博客
    如何写技术博客
    Spring + Spring MVC + Mybatis 框架整合
    Httpclient 4.5.2 请求http、https和proxy
    HttpClient4.5.2 连接池原理及注意事项
    php加密数字字符串,使用凯撒密码原理
    php 阿里云视频点播事件回调post获取不到参数
    Nginx代理后服务端使用remote_addr获取真实IP
    记录:mac的浏览器访问任何域名、网址都跳转到本地127.0.0.1或固定网址
  • 原文地址:https://www.cnblogs.com/slu182/p/4259017.html
Copyright © 2011-2022 走看看