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);
        }
    };

 



 

posted @ 2015-09-23 10:33  小小闹闹  阅读(906)  评论(0编辑  收藏  举报