【问题标题】:Google direction services.How to detect if geo location is disabled in the browser谷歌方向服务。如何检测浏览器中是否禁用了地理位置
【发布时间】:2012-02-03 09:49:14
【问题描述】:

如果用户禁用了位置功能,或者他使用了不受地理支持的功能。

如何以编程方式执行此操作?这将被执行,如果我在我的浏览器中启用地理位置,如果我不启用,我希望执行一些其他代码。我将在我不启用或使用不支持的浏览器时执行的其他代码放在哪里。

<script type="text/javascript">
  var directionDisplay;
  var directionsService = new google.maps.DirectionsService();
  var map;

  function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var chicago = new google.maps.LatLng(41.850033, -87.6500523);
    var myOptions = {
      zoom:7,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: chicago
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
  }

  function calcRoute() {
    var start = document.getElementById("start").value;
    var end = document.getElementById("end").value;
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  }
</script>
</head>
<body onload="initialize()">
<div>

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

    我会使用modernizer 来判断浏览器是否支持地理定位,然后采取适当的措施。

    if (Modernizr.geolocation) {
      //do geolocation/gmaps stuff
    } else {
      //do something else
    } 
    

    您甚至可以加载 shim 以将地理定位功能提供给不支持它的浏览器。 modernizer documentation section中有例子。

    例如

    Modernizr.load({
      test: Modernizr.geolocation,
      yep : 'geo.js',
      nope: 'geo-polyfill.js'
    });
    

    --编辑

    <html>
    <head>
    <script type="text/javascript"  src="modernizer.js"></script>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=&sensor=true"></script>
    <script>
    
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    
    function initialize() { 
    
      if (Modernizr.geolocation) {
      // Do the geolocation  
          navigator.geolocation.getCurrentPosition(initializeGeo, handle_error);          
      } else {
      // No geolocation path
      initializeNonGeo();
      } 
    }
    
    function handle_error(err) {
      alert("Geolocation could not be run");
      if (err.code == 1) {
        alert("user denied");// user said no!
      }
      initializeNonGeo();
    }
    
    function initializeGeo() {
      alert("has geolocation");
      directionsDisplay = new google.maps.DirectionsRenderer();
      var chicago = new google.maps.LatLng(41.850033, -87.6500523);
      var myOptions = {
        zoom:7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: chicago
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
    } 
    
    function initializeNonGeo() {
       alert("no geolocation"); //do non geo stuff.
    }
    </script>
    </head>
    <body onload="initialize()">
    <div id="map_canvas" style="width: 500px; height: 300px"></div>
    
    </body>
    </html>
    

    【讨论】:

    • 我可能会将您的 initilize 方法重命名为 initializeGeo 或类似的名称,然后创建一个类似的 initilize 方法:function initialize() { if (Modernizr.geolocation) { initiliseGeo(); } else { initialiseNonGeo } }
    • :我应该使用任何库来让该modernizr 正常工作吗?你不能在一个事件上调用两个函数
    • 您应该只需要 Modernizer 库 modernizr.com/download(您可能只需要检查额外的 Geolocation API 框)和 google maps API。您只需调用一种方法,然后使用 if else 语句调用适当的方法。
    • 只保留额外的默认值。 HTML5 shim、modernizer 加载和 CSS。
    • :我在 bot if 和 else 中插入了一个警报,两者都没有发出警报,这意味着插件无法正常工作。我是否有任何 Web 链接从该插件加载的位置。
    猜你喜欢
    • 2013-09-28
    • 2012-11-30
    • 2016-09-21
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多