zoukankan      html  css  js  c++  java
  • google map 定位

    在map初始化的过程中,得到当前经纬度,完成初始化地图,通过HTML5中的Geolocation实现,具体参考:http://www.jb51.net/html5/71556.html

    1.获取当前地理位置 
    调用方法 void getCurrentPosition(onSuccess, onError, options);即可。 
    其中onSuccess是获取当前位置信息成功时执行的回调函数,onError是获取当前位置信息失败时所执行的回调函数,options是一些可选熟悉列表。其中第二和第三个参数为可选属性。 
    在onSuccess回调函数中,用到了参数position,代表一个具体的position对象,表示当前位置。其具有如下属性: 
    •latitude:当前地理位置的纬度。 
    •longitude:当前地理位置的经度。 
    •altitude:当前位置的海拔高度(不能获取时为null)。 
    •accuracy:获取到的纬度和经度的精度(以米为单位)。 
    •altitudeAccurancy:获取到的海拔高度的经度(以米为单位)。 
    •heading:设备的前进方向。用面朝正被方向的顺时针旋转角度来表示(不能获取时为null)。 
    •speed:设备的前进速度(以米/秒为单位,不能获取时为null)。 
    •timestamp:获取地理位置信息时的时间。 

    在onError回调函数中,用到了error参数。其具有如下属性: 
    •code:错误代码,有如下值。 
    1.用户拒绝了位置服务(属性值为1); 
    2.获取不到位置信息(属性值为2); 
    3.获取信息超时错误(属性值为3)。 
    •message:字符串,包含了具体的错误信息。 

    在options参数中,可选属性如下: 
    •enableHighAccuracy:是否要求高精度的地理位置信息。 
    •timeout:设置超时时间(单位为毫秒)。 
    •maximumAge:对地理位置信息进行缓存的有效时间(单位为毫秒)。 

    结合angularjs代码如下:

    //google map 定位到当前位置
    fn.getLocation = function () {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(vm.showPosition, vm.showError);
    }
    else {
    $scope.error = "Geolocation is not supported by this browser.";
    }
    }
    vm.showPosition = function (position) {
    vm.lat = position.coords.latitude;
    vm.lng = position.coords.longitude;
    if(vm.lat!=undefined && vm.lng!=undefined){
    $scope.map = { center: { latitude: vm.lat, longitude: vm.lng }, zoom: 13 };
    }else{
    $scope.map = { center: { latitude: 38.203655, longitude: -98.613281 }, zoom: 5 };
    }
    $scope.options = {disableDefaultUI: true,scrollwheel:true,minZoom:5,maxZoom:21};
    /*console.log(position);*/

    }
    vm.showError = function (error) {
    switch (error.code) {
    case error.PERMISSION_DENIED:
    $scope.error = "User denied the request for Geolocation."
    break;
    case error.POSITION_UNAVAILABLE:
    $scope.error = "Location information is unavailable."
    break;
    case error.TIMEOUT:
    $scope.error = "The request to get user location timed out."
    break;
    case error.UNKNOWN_ERROR:
    $scope.error = "An unknown error occurred."
    break;
    }
    $scope.$apply();
    }
  • 相关阅读:
    Zookeeper数据类型
    Zookeeper基本命令
    Redis集群
    Mysql 模拟自增主键
    git回滚版本操作
    Redis缓存穿透和雪崩
    日期格式jackson格式化
    Zookeeper安装
    redis主从复制
    Redis哨兵模式
  • 原文地址:https://www.cnblogs.com/qyhol/p/5969734.html
Copyright © 2011-2022 走看看