Google Map API v3 - Zoom parameter don't work

时光总嘲笑我的痴心妄想 提交于 2019-12-31 07:33:08

问题


I'm using an Google API v3 but this parameter 'zoom : 10' is not working. Always I call the google api, the zoom is on max and I have to click on "-" about 5 times to get the desired zoom. I tried to put zoom: 1 and zoom: 100 but nothing happens. Maybe this parameter is being overwritten, but I can't see where.

Here is my code:

function viewOnMap(campus_name, lat, long, address1, address2){
  var locations = [ [ campus_name, lat, long, 1 ] ];

  var map = new google.maps.Map(document.getElementById('google_map'), {
    zoom : 10,
    center : new google.maps.LatLng(lat, long),  //lat long site
    mapTypeId : google.maps.MapTypeId.ROADMAP
  });
  var infowindow = new google.maps.InfoWindow();

  var marker, i;
  var bounds = new google.maps.LatLngBounds();
  for (i = 0; i < locations.length; i++) {
    marker = new google.maps.Marker(
            {
                position : new google.maps.LatLng(lat,
                        long),
                        map : map,
                        icon:'images/pin.png'
            });
    var pos = new google.maps.LatLng(lat, long);
    bounds.extend(pos);
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
                var content = '<div id="content">'+
                '<div id="siteNotice">Site name: '+ (campus_name) +
                '</div>'+
                '<div id="bodyContent">Address: '+ (address1) +
                '</div>'+
                '<div id="bodyContent1">'+ (address2) +
                '</div>'+
                '</div>';
            }
            infowindow.setContent(content);
            infowindow.open(map, marker);
        };
    })(marker, i));
  }
  map.fitBounds(bounds);    
}

Thanks!


回答1:


The call to google.maps.Map.fitBounds overrides the fixed zoom in the MapOptions.

If you only add one position to a google.maps.LatLngBounds, then use google.maps.Map.fitBounds, it will always zoom to the maximum possible zoom.

If you only have one position, don't use google.maps.Map.fitBounds.




回答2:


Have you tried. :

map.setZoom(10);

I would even try setting it after you set your markers.

In fact if you want to see if it works debug your code in chrome set a breakpoint after map has been rendered and run that code in the local window.



来源:https://stackoverflow.com/questions/28373032/google-map-api-v3-zoom-parameter-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!