【问题标题】:Remove fitBounds from google maps and set zoom level instead [duplicate]从谷歌地图中删除 fitBounds 并改为设置缩放级别[重复]
【发布时间】:2014-10-07 02:41:41
【问题描述】:

我已经尝试了所有方法,但无法弄清楚如何删除此代码的适合边界部分并改为设置缩放级别。问题是这张地图是为多个标记设计的,但我目前只使用 1 并且它放大得太近了。

var gmarkers = [];   
var markers = [
[   '<div id="mapcontent">' +
    '<a href ="#project0"><h4>43 Short Street</h4>' +
    '</div>'    
, -27.686478,153.131745]
];

function initializeMaps() {
var myOptions = {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: [{    featureType: 'all',  stylers: [{saturation: -100},{brightness: 5} ]}  ],
    scrollwheel: false    
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
var infowindow = new google.maps.InfoWindow(); 
var marker, i;
var bounds = new google.maps.LatLngBounds();

   google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
    });

for (i = 0; i < markers.length; i++) { 
    var pos = new google.maps.LatLng(markers[i][1], markers[i][2]);
    bounds.extend(pos);
    marker = new google.maps.Marker({
            icon: './img/mapmarker.png',
        position: pos,
        map: map
    });
    gmarkers.push(marker);
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(markers[i][0]);
            infowindow.open(map, marker);
        }


    })(marker, i));
}
map.fitBounds(bounds);

}
initializeMaps()
           function myClick(id){
    google.maps.event.trigger(gmarkers[id], 'click');
}

感谢您的帮助!

【问题讨论】:

    标签: google-maps-api-3


    【解决方案1】:

    改变

    map.fitBounds(bounds);
    

    到(或删除它)

    // map.fitBounds(bounds);
    

    在初始化地图时添加所需的缩放和中心:

    var myOptions = {
        // change these per your desired center and zoom.
        zoom: 4,
        center: new google.maps.LatLng(desiredLat, desiredLng),
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: [{    
          featureType: 'all',  
          stylers: [{
              saturation: -100
            },
            {
              brightness: 5
            }]
        }],
        scrollwheel: false    
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 
    

    但如果你只有一个标记,你也应该删除循环。

    【讨论】:

    • 谢谢!太明显了!不久将添加更多标记,因此此修复非常完美,因为我将反转更改。
    猜你喜欢
    • 2012-11-26
    • 2017-04-10
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 2012-09-08
    相关资源
    最近更新 更多