简体   繁体   English

为什么我看不到正确的Google JavaScript地图?

[英]Why can't i see right google javascript map?

I am using google maps javascript api in my web app to show maps. 我在网络应用程序中使用Google Maps JavaScript API来显示地图。 Following is the code which shows map: 以下是显示地图的代码:

 var map;
 var jsMap;

 $(function() {

      // doing stuff here 

      // showing map
      $('#map_div_1').append('<div id="map_1" style="width:640px; height:427px; margin-left:auto; margin-right:auto"></div>');
      var centerLatLng = new google.maps.LatLng(centerlat, centerlong);
      var myOptions = {
            center: centerLatLng,
            zoom: 17,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            draggable: true
        };

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

          // markerArray is initialized with values in beginning            
             var triangleCoords = new Array();

            for(var x=0;x<markerArray.length;x++){
                triangleCoords.push(new google.maps.LatLng(markerArray[x].latitude, markerArray[x].longitude));
                        }

            jsMap = new google.maps.Polygon({
                paths: triangleCoords,
                strokeColor: "#FF0000",
                strokeOpacity: 0.8,
                strokeWeight: 2,
                fillColor: "#FF0000",
                fillOpacity: 0.35
            });

            jsMap.setMap(map);

      // if this button will be clicked then i want to make another javascript google map in another div map_2
      $('#show_2nd_map').unbind("click");
      $('#show_2nd_map').bind('click', function(){
                // doing stuff
                  jsMap.setMap(null);
              jsMap = null;
              map = null;
               // showing 2nd map

               $('#map_div_2').append('<div id="map_2" style="width:640px; height:427px; margin-left:auto; margin-right:auto"></div>');

            var centerLatLng1 = new google.maps.LatLng(centerlat2, centerlong2);

            var myOptions = {
            center: centerLatLng1,
            zoom: 17,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            draggable: true
        };

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

         var triangleCoords2 = new Array();

         for(var x=0;x<markerArray2.length;x++){
                triangleCoords2.push(new google.maps.LatLng(markerArray2[x].latitude, markerArray2[x].longitude));
                        }

         jsMap = new google.maps.Polygon({
            paths: triangleCoords2,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: "#FF0000",
            fillOpacity: 0.35
         });

      jsMap.setMap(map);

      });
   });     

First map is created fine. 第一张地图创建良好。 But when i click show_2nd_map button then map is created but it doesn't show it right. 但是,当我单击show_2nd_map按钮时,会创建地图,但显示不正确。 i am attaching both screen shots. 我附上了两个屏幕截图。

第一张地图看起来像这样,正确

第二张地图看起来像这样,这是错误的

Google calculate size of the map based on map canvas element, so it need the canvas (div) to be visible before map initialize. Google根据地图画布元素计算地图的大小,因此在地图初始化之前需要画布(div)可见。 There is two walk around: 有两个漫步:

  1. Don't hide your canvas, just put them somewhere else (position absolute and left: -99999, top: -99999). 不要隐藏您的画布,只需将它们放在其他位置(绝对位置和左侧:-99999,顶部:-99999)。

  2. Recalculate width and height of the map google.maps.event.trigger(map, "resize"); 重新计算地图的宽度和高度google.maps.event.trigger(map, "resize");

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

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