分享一段Html5获取地理位置(定位)代码

来源:互联网 发布:mac hdmi声音输出设置 编辑:程序博客网 时间:2024/05/22 16:54
/** * 获取用户的地理位置 * @param cb:回调 */function getLocation(cb){var result = {};if (navigator.geolocation){        navigator.geolocation.getCurrentPosition(        //获取地址位置成功时回调        function(position){        if(position){        result.status = true;        result.data = position;        cb(result);        }else{        result.status = false;        cb(result);        }        },        //获取地址位置失败时回调        function(error){        result.status = false;        switch(error.code){            case error.TIMEOUT:            //超时                break;            case error.PERMISSION_DENIED:            //用户拒绝使用位置共享服务                break;            case error.POSITION_UNAVAILABLE:            //无法确定设备的地理位置                break;        }        cb(result);        }    );    }else{    result.status = false;    cb(result);    }}

0 0