web app 开发总结之三 获取用户地理位置 html5的window.navigator.geolocation

 

    一. window.navigator.geolocation的三个方法:     

  void getCurrentPosition(onSuccess,onError,options);
  //获取用户当前位置

  int watchCurrentPosition(onSuccess,onError,options);
  //持续获取当前用户位置

  void clearWatch(watchId);
  //watchId 为watchCurrentPosition返回的值
  //取消监控


options = {
enableHighAccuracy,//boolean 是否要求高精度的地理信息
timeout,//获取信息的超时限制
maximumAge//对地理信息进行缓存的时间
}
  //options可以不写,为默认即可

二、position对象

      当成功获取地理位置信息时候,onsuccess方法中会返回position对象,通过这个对象可以获取地理位置的相关信息,包括:

       position对象的属性:

latitude,//纬度

longitude,//经度

altitude,//海拔高度

accuracy,//获取纬度或者经度的精度

altitudeAccurancy,//海拔高度的精度

heading,//设备前景方向。正北方向的顺时针旋转角

speed,//设备的前进速度 m/s

timestamp,//获取地理位置信息时候的时间

三. 代码
//初始化地理位置
        if (sessionStorage.getLocationFlag != "1") {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(
                    function (position) {
                        var lat = position.coords.latitude;
                        var lng = position.coords.longitude;
                        var param = $.param({
                            longitude: lng,
                            latitude: lat
                        });
                        $.ajax({
                            url: ConfigHelper.ApiUrl() + "/area/ConvertLocationFromGps",     //根据经纬度得到城市
                            data: param,
                            type: "Post",
                            processData: false,
                            timeout: 10000,
                            dataType: "json",
                            contentType: "application/x-www-form-urlencoded; charset=utf-8",
                            success: function (data, textStatus) {
                                if (data && data.Response && data.Response.ErrorCode == 0) {
                                    if (data.Response.Result) {
                                        var item = data.Response.Result;
                                        var currenrLocation = { CityId: item.CityId, CityCode: item.CityCode, CityName: item.CityName, Longitude: item.Longitude, Latitude: item.Latitude, Address: item.Address };
                                        localStorage.currenrLocation = JSON.stringify(currenrLocation);
                                        sessionStorage.getLocationFlag = "1";
                                    }
                                } else {
                                    if (localStorage.currenrCity == undefined) {
                                        localStorage.currenrCity = JSON.stringify({ Id: 92, Name: "深圳" });
                                        $("#home_index #btnChangeCity").text("深圳");
                                    }
                                }
                            },
                            error: function () {
                                if (localStorage.currenrCity == undefined) {
                                    localStorage.currenrCity = JSON.stringify({ Id: 92, Name: "深圳" });
                                    $("#home_index #btnChangeCity").text("深圳");
                                }
                                $.mobile.changePage("#errorDialog", { transition: "pop", role: "dialog", changeHash: false });
                            },
                            complete: function () {
                                $.mobile.loading('hide');
                            }
                        });
                    },
                    function () {
                        if (localStorage.currenrCity == undefined) {
                            localStorage.currenrCity = JSON.stringify({ Id: 92, Name: "深圳" });
                            $("#home_index #btnChangeCity").text("深圳");
                        }
                    },
                    {
                        enableHighAcuracy: true,
                        timeout: 5000,
                        maximumAge: 3000
                    });
            } else {
                if (localStorage.currenrCity == undefined) {
                    localStorage.currenrCity = JSON.stringify({ Id: 92, Name: "深圳" });
                    $("#home_index #btnChangeCity").text("深圳");
                }
            }
        } else {
            if (localStorage.currenrCity == undefined) {
                localStorage.currenrCity = JSON.stringify({ Id: 92, Name: "深圳" });
                $("#home_index #btnChangeCity").text("深圳");
            }
        }
        //设置默认城市
        if (localStorage.currenrCity) {
            var currenrCity = JSON.parse(localStorage.currenrCity);
            $("#home_index #btnChangeCity").text(currenrCity.Name);
        }
    };

 



 

转载于:https://www.cnblogs.com/TierraPan/p/4831497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值