简体   繁体   English

为什么谷歌地图JavaScript第二次点击时没有正确显示地图?

[英]Why Google Maps JavaScript is not showing a map right when i click it second time?

I am using Google Maps JavaScript API v3 for showing locations on the map in my web application. 我正在使用Google Maps JavaScript API v3在我的网络应用程序中显示地图上的位置。 In my html I have: 在我的HTML中,我有:

<script src="jquery-mainpage.js"></script>
<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?key=AIzaSyB4LCwxrO5EzHnAQXCkP9fjREUEhOPCol4&sensor=false">
</script>

And this is how I am making a map when update button is clicked in jquery-mainpage.js : 这就是我在jquery-mainpage.js点击更新按钮时制作地图的方式:

$('#updatebtn').unbind("click");
$('#updatebtn').bind('click', function(){
    var keystring = $('#key').text();
    $.ajax({
        type: 'POST',
        url: 'http://localhost:8888/jsgooglemaps',
        data: {keystr: keystring},
        success: function(result) {
            var obj = jQuery.parseJSON(result);
            var centerLatLng = new google.maps.LatLng(obj.centerLat, 
                obj.centerLong);
            var myOptions = {
                center: centerLatLng,
                zoom: 17,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                draggable: true
            };

            var map = new google.maps.Map(
                document.getElementById("map_canvas_1"),
                myOptions);

            var markerArray = obj.locations;
            for(var i=0;i<markerArray.length;i++){
                var myLatlng = new google.maps.LatLng(
                    markerArray[i].latitude, 
                    markerArray[i].longitude);
                var marker = new google.maps.Marker({
                    clickable: true,
                    position: myLatlng,
                    map: map,
                    zIndex: i
                });
                google.maps.event.addListener(marker, "click", function() {
                    var tempLatLng = new google.maps.LatLng(0, 0);
                    this.setVisible(false); 
                    this.setPosition(tempLatLng);
                    var j = this.getZIndex();
                    markerArray[j].latitude = 0;
                    markerArray[j].longitude = 0;
                });
            }
        }
    });
});

On first click it shows everything right. 首次点击它会显示一切正确。 but when i click update button again then it doesn't show map right. 但是当我再次点击更新按钮时,它不会显示正确的地图。 I am attaching both screen shots. 我正在附上两个屏幕截图。 Can anybody tell me why is it happening. 任何人都可以告诉我它为什么会发生。 Thanks in advance. 提前致谢。 Map on first click 首次点击地图 在此输入图像描述

Map when i click update button again 当我再次点击更新按钮时映射 在此输入图像描述

You don't want to repeatedly create a new instance of the map, because there is a known bug that prevents the memory from previous map instances from being garbage collected. 您不希望重复创建映射的新实例,因为存在一个已知的错误,该错误会阻止以前的映射实例的内存被垃圾回收。 Check out this question and the discussion and resulting answers: What is the Proper Way to Destroy a Map Instance? 看看这个问题以及讨论和得到的答案: 什么是破坏地图实例的正确方法?

In the answer to that question, there is a link to a video presented during a Google Maps Office Hours session by Chris Broadfoot and Luke Mahe from the Google Maps team that explains they do not support use cases that involve repeated creation of map instances. 在该问题的答案中,Google地图团队的Chris Broadfoot和Luke Mahe在谷歌地图办公时间会议期间提供了一个视频链接,该视频解释了他们不支持涉及重复创建地图实例的用例。 You will be much better off keeping a single instance of the map and reusing the same map throughout your user's session. 保留单个地图实例并在整个用户会话中重复使用相同的地图会更好。

I am creating map_convas_1 div from the ajax success code and removing it when updating is done. 我正在从ajax成功代码创建map_convas_1 div并在更新完成后将其删除。 and then creating it again whenever user clicks update button. 然后在用户单击更新按钮时再次创建它。 Its working fine. 它的工作正常。

It works. 有用。 Thank you very much, your "remove code from div" option works for me, my problem was that second time it doesn't work, if I'm resizing the screen it works but not is a natural way that user resize the screen to see the map. 非常感谢,你的“从div中删除代码”选项适用于我,我的问题是第二次它不起作用,如果我正在调整屏幕大小它工作但不是用户调整屏幕大小的自然方式看地图。

I'm using Jquery to remove div code. 我正在使用Jquery删除div代码。 In my case I was using the jquery dialog, then when I close the dialog I'm replacing the html from the div (where google maps is showing the map) with the original div code. 在我的情况下,我使用的是jquery对话框,然后当我关闭对话框时,我正在使用原始div代码替换div中的html(谷歌地图显示地图)。

My original div code was 我原来的div代码是

<div id="mapaLocalsub" style="width:500px; height:450px; " ></div>

I notice that after google maps was working it fills the div with a lot of things and style, then in the close function from my Jquery.dialog I'm replacing the html with my original div code: 我注意到谷歌地图工作后它用很多东西和风格填充div,然后在我的Jquery.dialog的close函数中我用我原来的div代码替换html:

$( "#mapaLocal" ).dialog({
      height: 450,
      width: 500,
      modal: true,
      close: function() {
      $( "#mapaLocal" ).html("<div id=\"mapaLocalsub\" style=\"width:500px;     height:450px; \" ></div>");
      }
});

But of course, you can use the Jquery html method in a function that you call when close the function or before creating your map, hope it helps. 但是,当然,您可以在关闭函数时或创建映射之前调用的函数中使用Jquery html方法,希望它有所帮助。

$( "#mapaLocal" ).html("<div id=\"mapaLocalsub\" style=\"width:500px;     height:450px; \" ></div>");

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

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