【问题标题】:Google maps project does not work on mobile devices谷歌地图项目不适用于移动设备
【发布时间】:2015-09-24 18:49:01
【问题描述】:

我使用 Google Map 的 API 构建了一张地图。它就像我想要的那样在桌面上工作,但它不能在移动设备上加载。我的网站是响应式的,我将地图画布的宽度设置为 100%。我读了一些帖子,建议我需要摆脱我的尺寸样式才能让它工作,但我没有运气。谁能帮我看这张地图?谢谢

这是我运行代码的网页。 http://stlresidences.com/test-map

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<style>
 #googft-mapCanvas {
    height: 600px;
    margin: 0;
    padding: 0;
    width: 100%;
}
.controls {
    margin-top: 10px;
    border: 1px solid transparent;
    border-radius: 2px 0 0 2px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    height: 32px;
    outline: none;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
    background-color: #fff;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    margin-left: 12px;
    padding: 0 11px 0 13px;
    text-overflow: ellipsis;
    width: 300px;
}
#pac-input:focus {
    border-color: #4d90fe;
}
.pac-container {
    font-family: Roboto;
}
#type-selector {
    color: #fff;
    background-color: #4d90fe;
    padding: 5px 11px 0px 11px;
}
#type-selector label {
    font-family: Roboto;
    font-size: 13px;
    font-weight: 300;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Enter Address Here">
<div id="googft-mapCanvas"></div>
<script>
function initialize() {
    google.maps.visualRefresh = true;
    var isMobile = (navigator.userAgent.toLowerCase().indexOf('android') > -1) || (navigator.userAgent.match(/(iPod|iPhone|iPad|BlackBerry|Windows Phone|iemobile)/));
    if (isMobile) {
        var viewport = document.querySelector("meta[name=viewport]");
        viewport.setAttribute('content', 'initial-scale=1.0, user-scalable=no');
    }

    var mapDiv = document.getElementById('googft-mapCanvas');
    mapDiv.style.width = isMobile ? '100%' : '1000px';
    mapDiv.style.height = isMobile ? '100%' : '300px';
    var map = new google.maps.Map(mapDiv, {
        center: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.HYBRID
    });
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend-open'));
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('googft-legend'));

    layer = new google.maps.FusionTablesLayer({
        map: map,
        heatmap: {
            enabled: false
        },
        query: {
            select: "col26",
            from: "11Q0B7iRayT2JIOBl8_VRUmitimhX1W01byuFDnAv",
            where: ""
        },
        options: {
            styleId: 2,
            templateId: 2
        }
    });
    if (isMobile) {
        var legend = document.getElementById('googft-legend');
        var legendOpenButton = document.getElementById('googft-legend-open');
        var legendCloseButton = document.getElementById('googft-legend-close');
        legend.style.display = 'none';
        legendOpenButton.style.display = 'block';
        legendCloseButton.style.display = 'block';
        legendOpenButton.onclick = function () {
            legend.style.display = 'block';
            legendOpenButton.style.display = 'none';
        }
        legendCloseButton.onclick = function () {
            legend.style.display = 'none';
            legendOpenButton.style.display = 'block';
        }
    }
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(38.64936217820852, -90.53628850000001),
        map: map,
        draggable: true,
        title: "Your New Home",
    });
     // Create the search box and link it to the UI element.
  var input = document.getElementById('pac-input');
  var searchBox = new google.maps.places.SearchBox(input);
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  // Bias the SearchBox results towards current map's viewport.
  map.addListener('bounds_changed', function() {
    searchBox.setBounds(map.getBounds());
  });

  // Listen for the event fired when the user selects a prediction and retrieve
  // more details for that place.
  searchBox.addListener('places_changed', function() {
    var places = searchBox.getPlaces();
    if (places.length == 0) {
      return;
    }
    // For each place, get the icon, name and location.
    var bounds = new google.maps.LatLngBounds();
    places.forEach(function(place) {
      if (place.geometry.viewport) {
        // Only geocodes have viewport.
        map.fitBounds(place.geometry.viewport);
      } 
      else {
         map.setCenter(place.geometry.location);
      map.setZoom(11);  // Why 17? Because it looks good.
      }
      // now let's move the marker
      marker.setPosition(place.geometry.location);
    });
    //map.fitBounds(bounds);
    map.panTo(marker.getPosition());
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<body>
</body>
</html>

【问题讨论】:

    标签: javascript google-maps mobile google-maps-api-3 responsive-design


    【解决方案1】:

    您已从 FusionTable 页面复制代码,#googft-mapCanvas 的样式将被覆盖。

    删除这两行:

    mapDiv.style.width = isMobile ? '100%' : '1000px';
    mapDiv.style.height = isMobile ? '300px' : '300px';
    

    也删除这个(你的页面中没有#googft-legend

    if (isMobile) {
        var legend = document.getElementById('googft-legend');
        var legendOpenButton = document.getElementById('googft-legend-open');
        var legendCloseButton = document.getElementById('googft-legend-close');
        legend.style.display = 'none';
        legendOpenButton.style.display = 'block';
        legendCloseButton.style.display = 'block';
        legendOpenButton.onclick = function () {
            legend.style.display = 'block';
            legendOpenButton.style.display = 'none';
        }
        legendCloseButton.onclick = function () {
            legend.style.display = 'none';
            legendOpenButton.style.display = 'block';
        }
    }
    

    此外:加载 API 的发布版本

    <script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=geometry,places"></script>
    

    目前您加载的实验 API 版本似乎非常不稳定

    【讨论】:

      【解决方案2】:

      您的问题可能是地图的 DIV 大小有些动态,并且 Google 地图无法在加载时绘制。我建议您在页面加载后一段时间(例如 100 毫秒)尝试运行它,看看它是否有帮助。根据 Google 的建议,这通常是响应式地图的最佳解决方案:

      google.maps.event.trigger(map, 'resize')
      

      这段代码所做的基本上是让地图重新绘制自己,因为它的容器大小刚刚改变。

      【讨论】:

        猜你喜欢
        • 2016-05-24
        • 1970-01-01
        • 1970-01-01
        • 2017-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-01
        • 2012-10-03
        相关资源
        最近更新 更多