Javascript 移动设备上的谷歌地图缩放级别
我已经做了一个谷歌地图与API。地图正常工作,但我对移动设备上的缩放级别不满意。我想放大更多,但只在手机上 是否可以仅在手机上更改缩放 HTMLJavascript 移动设备上的谷歌地图缩放级别,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我已经做了一个谷歌地图与API。地图正常工作,但我对移动设备上的缩放级别不满意。我想放大更多,但只在手机上 是否可以仅在手机上更改缩放 HTML <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div id="map"></div> </div> </div> Javascript <script> functio
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div id="map"></div>
</div>
</div>
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>
函数initMap(){
var中心={lat:56.463415,lng:9.495170};
变量位置=[
['Headline','Headline
Address
',1.686340,15.998270],
];
var map=new google.maps.map(document.getElementById('map'){
缩放:7,
中心:中心
});
var infowindow=new google.maps.infowindow({});
var标记,计数;
对于(计数=0;计数
您可以通过(window.screen.width*window.devicePixelRatio)和类似的高度(window.screen.height*window.devicePixelRatio)获得显示宽度
然后,您可以使用浏览器中的开发工具检查地图变小时的最大分辨率
最后,更改该条件的缩放特性。例如:
var screenWidth = window.screen.width * window.devicePixelRatio;
var screenHeight = window.screen.height * window.devicePixelRatio;
if (screenWidth < 600 && typeof map !== "undefined")
{
map.setZoom(10);
}
var screenWidth=window.screen.width*window.devicePixelRatio;
var screenHeight=window.screen.height*window.devicePixelRatio;
如果(屏幕宽度<600&&typeof地图!=“未定义”)
{
map.setZoom(10);
}
检测移动设备的另一种方法是使用library。非常感谢您的建议。我只是在测试它,但无法让它工作。在javascript中,您将在哪里设置它?也许我插入的代码不正确?请用您尝试过的内容更新您的问题,并解释哪些不起作用。你试过调试吗?此外,上面发布的代码在我编辑之前部分错误。再次选中。MK_DK,您可以将代码粘贴到代码段的末尾。只需将其放置在已经存在google.maps.Map对象的位置。
var screenWidth = window.screen.width * window.devicePixelRatio;
var screenHeight = window.screen.height * window.devicePixelRatio;
if (screenWidth < 600 && typeof map !== "undefined")
{
map.setZoom(10);
}