简体   繁体   English

Google地图无法完全加载

[英]Google Maps don't fully load

I have a somewhat strange problem. 我有一个奇怪的问题。 I have two maps on my site, a big one and a small one. 我的网站上有两张地图,一张大地图和一张小地图。 I want to use the big one to show a route to a certain address. 我想用大的一个来显示到某个地址的路线。 I'm now trying to implement the two maps but get a weird problem. 我现在正在尝试实现这两张地图,但却遇到了一个奇怪的问题。 The small map is working fine, but on the big map only a small area of the div is filled with the map, the rest is empty. 小地图工作正常,但在大地图上只有div的一小块区域填充了地图,其余的都是空的。 (See the image.) (见图。)

谷歌地图错误

I use the following code to display the two maps: 我使用以下代码显示两个映射:


   function initialize() {
    var latlng = new google.maps.LatLng(51.92475, 4.38206);
    var myOptions = {zoom: 10, center: latlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var marker = new google.maps.Marker({position: latlng, map:map, title:"Home"});
    var image = '/Core/Images/Icons/citysquare.png';
    var myLatLng = new google.maps.LatLng(51.92308, 4.47058);
    var cityCentre = new google.maps.Marker({position:myLatLng, map:map, icon:image, title:"Centre"});
    marker.setMap(map);

    var largeLatlng = new google.maps.LatLng(51.92475, 4.38206);
    var largeOptions = {zoom: 10, center: largeLatlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
    var largeMap = new google.maps.Map(document.getElementById("largeMap"), largeOptions);
    var largeMarker = new google.maps.Marker({position: largeLatlng, map:largeMap, title:"Cherrytrees"});
    largeMarker.setMap(largeMap); 
   }
   [..]
   jQuery(document).ready(function () {
    [..]
    initialize();
   });

What's going wrong here? 这里出了什么问题?

EDIT: 编辑:

Unfortunately the suggestions below doesn't seem to work. 不幸的是,下面的建议似乎不起作用。 The closes i came is to remove the display:none from the elements and set the elements to hide with jquery 我来的关闭是从元素中删除display:none并将元素设置为使用jquery隐藏


   [..]
   jQuery(document).ready(function () {
    [..]
    $("#shadow").add($("#shadowContent"),$("#closebar"),$("#content")).hide();
   });

With the following result 具有以下结果 替代文字

Yes, @Argiropoulos-Stavros but, Add it as a listener 是的,@ Argiropoulos-Stavros但是,将它添加为听众

    google.maps.event.addListenerOnce(map, 'idle', function(){
        google.maps.event.trigger(map, 'resize');
        map.setCenter(location);
    });

It will begin re-sizing after, map rendered. 它将在地图渲染之后开始重新调整大小。

I think you are using v3. 我认为你正在使用v3。 So google.maps.event.trigger(map, "resize"); 所以google.maps.event.trigger(map,“resize”);

Also take a look at here 另外看看这里

I fixed it! 我修好了它!

I made an own function for the largemap and placed it in the callback when the elements are opened 我为largemap创建了一个自己的函数,并在元素打开时将它放在回调中


function largeMap(){
 var largeLatlng = new google.maps.LatLng(51.92475, 4.38206);
 var largeOptions = {zoom: 10, center: largeLatlng,mapTypeId: google.maps.MapTypeId.ROADMAP};
 var largeMap = new google.maps.Map(document.getElementById("largeMap"), largeOptions);
 var largeMarker = new google.maps.Marker({position: largeLatlng, map:largeMap, title:"Cherrytrees"});
 largeMarker.setMap(largeMap);
}
[..]
 $("#showRoute").click(function(e){
  e.preventDefault();
  $("#shadow").add($("#shadowContent"),$("#closebar"),$("#content")).fadeIn(500);
  $("#shadowContent").show().css({'width':'750px','top':'25px','left':'50%','margin-left':'-400px'});
  $("#closeBarLink").click(function(l){
   l.preventDefault();
   $("#shadow").add($("#shadowContent"),$("#closebar"),$("#content")).fadeOut(500);
  });
  largeMap();
 });

Thanks anyway!! 不管怎么说,还是要谢谢你!!

call initialize(); call initialize(); function after you box active. 框激活后的功能。

$(document).ready(function () {
  $("#shadow").show(function () {
    initialize();
  })
});

When you're loading in the large map, try adding this at the end of your map code. 当您在大地图中加载时,请尝试在地图代码的末尾添加此地图。

map.checkResize();

When Google Maps first renders on your page, it has the dimensions recorded so that it displays the map images in that size. 当Google地图首次在您的网页上呈现时,它会记录尺寸,以便以该尺寸显示地图图像。 If you're resizing the map dynamically, you need to tell the API to check the new size. 如果要动态调整地图大小,则需要告诉API检查新大小。

<script type='text/javascript' >
var geocoder, map;
function codeAddress() {
    var address= '<?php echo $this->subject()->address; ?>';
    geocoder = new google.maps.Geocoder();
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var myOptions = {
                zoom: 13,
                center: results[0].geometry.location,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon:'http://rvillage.com/application/themes/rvillage/images/map-marker.png'
            }); 
            var infowindow = new google.maps.InfoWindow({ content: 'coming soon' });
            google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map, this);         
            });
        }
    });
}

jQuery(document).ready(function () {
    codeAddress();
});
</script>

You are using pop up window with jQuery, and I guest you call initialize() function when document is ready ( $(document).ready(function() {initialize(); }) ). 你正在使用带有jQuery的弹出窗口,并且我在guest文件准备好时调用initialize()函数( $(document).ready(function() {initialize(); }) )。 Try call initialize() function after pop up windown showed. 弹出窗口显示后尝试调用initialize()函数。

Johnny 约翰尼

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

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