Maps JavaScript API-地理位置标记未在移动设备上显示

时间:2019-03-20 17:28:44

标签: javascript google-maps geolocation

我正在试验Google Maps Platform,特别是具有Maps JavaScript API功能documentation seen hereGeolocation

我的代码在SSL上的heroku上运行,在使用Web浏览器(但是我通过android移动设备访问该应用程序)时可以很好地查明我的位置;地图会加载,但是没有,但是我的位置没有标记。(尽管勾选了允许位置跟踪)。

我想对下面的代码做些什么进一步的规定。

代码

<!DOCTYPE html>
<html>
  <head>

    <title>ConDodge Updated</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>

  </head>
  <body>
    <div id="map"></div>

        <script>
          // Maps JavaScript API
          // Runs on Google Maps Platform under Geolocation

          // Note:  If you see the error "The Geolocation service
          // failed.", it means you have to use a live server that is https enabled and not http
          // ie, herouku 

          var map, infoWindow;          
            function initMap() {
              map = new google.maps.Map(document.getElementById('map'), {
                center: {lat: -34.397, lng: 150.644},
                zoom: 6
              });
              infoWindow = new google.maps.InfoWindow;

            // Try HTML5 geolocation.
            if (navigator.geolocation) { // the navigator.geolocation property determines users location
              navigator.geolocation.getCurrentPosition(function(position) {
                var pos = {
                  lat: position.coords.latitude,
                  lng: position.coords.longitude
                };

                infoWindow.setPosition(pos);
                infoWindow.setContent('Location found.');
                infoWindow.open(map);
                map.setCenter(pos);
              }, function() {
                handleLocationError(true, infoWindow, map.getCenter());
              });
            } else {
              // Browser doesn't support Geolocation
              handleLocationError(false, infoWindow, map.getCenter());
            }
          }

          function handleLocationError(browserHasGeolocation, infoWindow, pos) {
            infoWindow.setPosition(pos);
            infoWindow.setContent(browserHasGeolocation ?
                                  'Error: The Geolocation service failed.' :
                                  'Error: Your browser doesn\'t support geolocation.');
            infoWindow.open(map);
          }

          //app will detect iPhone and Android devices and scale accordingly

          function detectBrowser() {
            var useragent = navigator.userAgent;
            var mapdiv = document.getElementById("map");

            if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
              mapdiv.style.width = '100%';
              mapdiv.style.height = '100%';
            } else {
              mapdiv.style.width = '600px';
              mapdiv.style.height = '800px';
            }
          }
        </script>

    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=blablablablablabal&region=GB&callback=initMap">
    </script>

  </body>
</html>

0 个答案:

没有答案
相关问题