简体   繁体   English

设置Google Maps API V3根据屏幕/设备宽度缩放级别

[英]Setting Google Maps API V3 Zoom level based on screen/device width

I have the following code which display 3 markers on a google map. 我有以下代码在谷歌地图上显示3个标记。 On screen widths larger than 600px the zoom level is set to '13', which is fine. 在屏幕宽度大于600px时,缩放级别设置为“13”,这很好。 Ideally, once the screen/device width is lower than 600px I would like the zoom level to drop to '12'. 理想情况下,一旦屏幕/设备宽度低于600px,我希望缩放级别降至“12”。

I have tried using fitBounds() , and looked at LatLngBounds , but I'm struggling to get those to work with my code. 我已经尝试使用fitBounds() ,并查看了LatLngBounds ,但我很难让这些与我的代码一起工作。 Is this simple to achieve? 这很简单吗? Any help would be grately appreciated. 任何帮助都会受到赞赏。

My code is below: 我的代码如下:

 var map; var locations = [ ['Location 1', 53.560410,-2.105351, 2], ['Location 2', 53.532154, -2.165793, 1], ['Location 3', 53.5386671,-2.1681325, 3] ]; function bindInfoWindow(marker, map, infowindow, strDescription) { google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(strDescription); infowindow.open(map, marker); }); } function setMarkers(map, locations) { var infowindow = new google.maps.InfoWindow({ content: "" }); var i, location, myLatLng, marker; for (i = 0; i < locations.length; i++) { location = locations[i]; myLatLng = new google.maps.LatLng(location[1], location[2]); marker = new google.maps.Marker({ position: myLatLng, map: map, title: location[0], zIndex: location[3] }); bindInfoWindow(marker, map, infowindow, location[0]); } } function initialize() { var mapOptions = { zoom: 13, center: new google.maps.LatLng(53.545701, -2.131714), panControl: false, disableDefaultUI: true, zoomControl: true, scrollwheel: false, panControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); setMarkers(map, locations); } google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addDomListener(window, 'resize', initialize); window.onpopstate = initialize(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <div id="map-canvas" style="height:400px; width:100%;"></div> 

To use fitBounds: 要使用fitBounds:

  1. create a google.maps.LatLngBounds object 创建一个google.maps.LatLngBounds对象
  2. add all the markers positions to it 添加所有标记位置
  3. call map.fitBounds 调用map.fitBounds

code snippet: 代码段:

 var map; var bounds = new google.maps.LatLngBounds(); var locations = [ ['Location 1', 53.560410,-2.105351, 2], ['Location 2', 53.532154, -2.165793, 1], ['Location 3', 53.5386671,-2.1681325, 3] ]; function bindInfoWindow(marker, map, infowindow, strDescription) { google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(strDescription); infowindow.open(map, marker); }); } function setMarkers(map, locations) { var infowindow = new google.maps.InfoWindow({ content: "" }); var i, location, myLatLng, marker; for (i = 0; i < locations.length; i++) { location = locations[i]; myLatLng = new google.maps.LatLng(location[1], location[2]); marker = new google.maps.Marker({ position: myLatLng, map: map, title: location[0], zIndex: location[3] }); bounds.extend(marker.getPosition()); bindInfoWindow(marker, map, infowindow, location[0]); } } function initialize() { var mapOptions = { zoom: 13, center: new google.maps.LatLng(53.545701, -2.131714), panControl: false, disableDefaultUI: true, zoomControl: true, scrollwheel: false, panControl: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); setMarkers(map, locations); map.fitBounds(bounds); } google.maps.event.addDomListener(window, 'load', initialize); google.maps.event.addDomListener(window, 'resize', initialize); window.onpopstate = initialize(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <div id="map-canvas" style="height:400px; width:100%;"></div> 

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

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