简体   繁体   English

带有Google Maps API的Mobile上的地理位置服务失败

[英]Geolocation service failed on Mobile with google maps api

I am using the following script on my site: 我在网站上使用以下脚本:

<script type="text/javascript">
    var map;

    function initialize() {
      var mapOptions = {
        zoom: 10,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
      var kmlLayer = new google.maps.KmlLayer("https://maps.google.ca/maps/ms?ie=UTF8&msa=0&output=kml&msid=210000746142049190989.0004ce14b7a8e95167602");
    kmlLayer.setMap(map);

      // Try HTML5 geolocation
      if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
          var pos = new google.maps.LatLng(position.coords.latitude,
                                           position.coords.longitude);

          var infowindow = new google.maps.InfoWindow({
          map: map,
          position: pos,
          content: 'Location found using HTML5.'
          });
           map.setZoom(14);
          map.setCenter(pos);
        }, function() {
          handleNoGeolocation(true);
        });
      } else {
        // Browser doesn't support Geolocation
        handleNoGeolocation(false);
      }
    }

    function handleNoGeolocation(errorFlag) {
      if (errorFlag) {
        var content = 'Error: The Geolocation service failed.';
      } else {
        var content = 'Error: Your browser doesn\'t support geolocation.';
      }

      var options = {
        map: map,
        position: new google.maps.LatLng(60, 105),
        content: errorFlag
      };

      var infowindow = new google.maps.InfoWindow(options);
      map.setCenter(options.position);
    }

    google.maps.event.addDomListener(window, 'load', initialize);

        </script>

I am also using the following which is needed for mobile 我还使用了移动设备所需的以下内容

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>

But still when I visit the site on mobile, and I click to view the map I get the error "The Geolocation service failed." 但是当我通过移动设备访问该站点时,仍然单击并查看地图,但仍收到错误消息“地理位置服务失败”。

Is there somthing else I am missing for the geo-location to work on mobile? 在移动设备上工作时,地理位置还有其他我想念的东西吗?

You can see the site and live example here on the site, right hand side if you scroll down just past the fold you will see "where can I get rumble?" 您可以在此处的站点上看到该站点和实时示例,如果您向下滚动到折叠处,您会在右侧看到“我能在哪里发出隆隆声?”。 click on that for the map. 单击该地图。

EDIT I think it has somthing to do with the kmlLayer it seems to be defaulting to that on mobile? 编辑我认为它与kmlLayer有关,似乎默认为移动设备上的默认值吗?

var kmlLayer = new google.maps.KmlLayer("https://maps.google.ca/maps/ms?ie=UTF8&msa=0&output=kml&msid=210000746142049190989.0004ce14b7a8e95167602");
kmlLayer.setMap(map);

According to Google Maps zoom gets overridden, when using a kml file 根据Google Maps,使用kml文件时缩放被覆盖

in the function initialize, replace all you have before // Try HTML5 geolocation with: 在初始化函数中,将之前的所有内容替换为// Try HTML5 geolocation为:

var mapOptions = { 
    zoom: 8, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions); 
var kmlLayer = new google.maps.KmlLayer("https://maps.google.ca/maps/ms?ie=UTF8&msa=0&output=kml&msid=210000746142049190989.0004ce14b7a8e95167602", { map: map, preserveViewport: true });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM