Major Google Maps glitch using API V3

£可爱£侵袭症+ 提交于 2020-02-06 03:58:27

问题


edit: this is now solved, see my answer below


The situation:

  1. User clicks on a map-image
  2. google maps API V3 is loaded via ajax
  3. the map is shown in a dialog window / lightbox

What happens:

The map displays and all functionality works however there's a glitch with the top-left 'square' of the map.

I'm stuck!


edit: now with code:

<div id="map_canvas"></div>
<script type="text/javascript">
    $(function() {          
            var latlng = new google.maps.LatLng(51.448359,-2.590559);
            var options = {
              zoom: 13,
              center: latlng,
              mapTypeControl: false,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }; 

            var map = new google.maps.Map(document.getElementById('map_canvas'), options);

            var marker = new google.maps.Marker({
              position: new google.maps.LatLng(51.448359,-2.590559),
              map: map
            });
    })
</script>

回答1:


Thanks to Alphonso for pointing me in the correct direction.

The problem is in the dimensions of the map div, even though #map_canvas had a height and width applied via css in the top of the document, it would seem the google maps API initializing before the style was being applied (this theory could be tested with dynatrace).

*Easy solution:

1) inline styles for width and height*

<div id="map_canvas" style="width: 700px; height: 400px"></div>

2) delay the loading of the map with setTimeout()




回答2:


I think that the problem is that the map initializes before the resizing animation has finished and so it thinks that it's smaller.

Try to make it adjust its size after the loading effect has finished:

google.maps.event.trigger(map, 'resize');

(you'll have to keep a reference to the "map" object



来源:https://stackoverflow.com/questions/3536998/major-google-maps-glitch-using-api-v3

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