简体   繁体   English

移动设备上的 Google 地图缩放级别

[英]Google Maps zoom level on mobile

I have made a google maps with the API.我已经用 API 制作了一个谷歌地图。 The map is working as it should, but I am not satisfied with the zoom level on mobile devices.地图正常工作,但我对移动设备上的缩放级别不满意。 I would like to zoom more out, but only on mobile.我想缩小更多,但仅限于移动设备。

Is it possible to change the zoom only on mobile?是否可以仅在移动设备上更改缩放比例?

HTML HTML

<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-12">
    <div id="map"></div>
  </div>
</div>

CSS CSS

#map {
  height: 400px;
  width: 100%;
  background-color: grey;
}

Javascript Javascript

<script>
  function initMap() {
    var center = {lat: 56.463415, lng: 9.495170};
    var locations = [

      ['Headline', 'Headline<br>Address<br><a href="#" target="_blank">Show direction</a>', 1.686340, 15.998270],

    ];

    var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 7,
    center: center
    });
    var infowindow = new google.maps.InfoWindow({});
    var marker, count;
    for (count = 0; count < locations.length; count++) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[count][2], locations[count][3]),
        map: map,
        title: locations[count][0]
      });
      google.maps.event.addListener(marker, 'click', (function(marker, count) {
        return function() {
          infowindow.setContent(locations[count][1]);
          infowindow.open(map, marker);
        }
      })(marker, count));
    }
  }
  google.maps.event.addDomListener(window, 'load', initMap);
</script>

You would get the display width by (window.screen.width * window.devicePixelRatio) and similarly the height (window.screen.height * window.devicePixelRatio).您可以通过 (window.screen.width * window.devicePixelRatio) 和类似的高度 (window.screen.height * window.devicePixelRatio) 获得显示宽度。

Then you could eg check the max resolution on which your map becomes too small using dev tools in your browser.然后,您可以使用浏览器中的开发工具检查地图变得太小的最大分辨率。

At the end change the zoom property of that condition.最后更改该条件的缩放属性。 For example:例如:

var screenWidth = window.screen.width * window.devicePixelRatio;
var screenHeight = window.screen.height * window.devicePixelRatio;
if (screenWidth < 600 && typeof map !== "undefined")
{
    map.setZoom(10);
}

Other way to detect mobile devices would be using http://www.modernizr.com/ library.检测移动设备的其他方法是使用http://www.modernizr.com/库。

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

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