仅显示部分地图API V3问题

发布于 2024-11-18 09:18:37 字数 1299 浏览 6 评论 0原文

我想在一页上使用两个(或更多)地图。(使用 telerik Grid) iterik grid 中的两行

如您所见,如果我尝试打开一行的“详细信息”(可能是第一行、第二行或最后一行)地图将正确显示。如果尝试展开另一行的详细信息,地图将显示无效。我无法理解我做错了什么?

 function onTabSelect(e) {
//...
                    if (node.childNodes.length == 0) {// If Coordinates tab not contain map
                    map = document.createElement('DIV');
                    map.setAttribute('id', selectedGeoObject.Id + "_gmap"); //Generates Id for div container
                    map.setAttribute('style', 'width:400px; height:278px; margin : 0px; padding : 0px;');
                    node.appendChild(map);
                    initMap(map);
                }
}
    function initMap(gMapContainer) {
        var myLatlng = new google.maps.LatLng(51.0, 9.0);
        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        //width:400px; height:278px;
        var gMap = new google.maps.Map(gMapContainer, myOptions);
        // Can not solve the problem.
        //google.maps.event.trigger(gMap, 'resize');
        //gMap.setCenter(myLatlng);
        return gMap;
    }

如果我尝试调整浏览器窗口的大小,两个地图都会正确显示(我已经在 Firefox、Opera、Chrome 中尝试过此操作)。你能帮我解决这个奇怪的错误吗?

I want to use two(or more) maps on one page.(using telerik Grid)
its two rows in telerik grid

As you see, If I will try to open "details" for one row(It could be first, second, or last row) map will be displayed correctly. If will try to expand details for another row, map will be displayed not valid. I cannot understand what have I done wrong?

 function onTabSelect(e) {
//...
                    if (node.childNodes.length == 0) {// If Coordinates tab not contain map
                    map = document.createElement('DIV');
                    map.setAttribute('id', selectedGeoObject.Id + "_gmap"); //Generates Id for div container
                    map.setAttribute('style', 'width:400px; height:278px; margin : 0px; padding : 0px;');
                    node.appendChild(map);
                    initMap(map);
                }
}
    function initMap(gMapContainer) {
        var myLatlng = new google.maps.LatLng(51.0, 9.0);
        var myOptions = {
            zoom: 12,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        //width:400px; height:278px;
        var gMap = new google.maps.Map(gMapContainer, myOptions);
        // Can not solve the problem.
        //google.maps.event.trigger(gMap, 'resize');
        //gMap.setCenter(myLatlng);
        return gMap;
    }

If I will try to resize my browser window, both maps will be displayed correctly(I've tried this in Firefox, Opera, Chrome). Could you help me with this strange bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似梦非梦 2024-11-25 09:18:37

在窗口调整大小时调用此方法:

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

像这样:(请注意,您需要通过 document.getElementByID 等获取 gMap 句柄)

window.onresize = function(event) {
 google.maps.event.trigger(gMap, 'resize');   
}

Call this on the window resize:

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

Like So: ( Note you'll need the gMap handle via document.getElementByID etc )

window.onresize = function(event) {
 google.maps.event.trigger(gMap, 'resize');   
}
以为你会在 2024-11-25 09:18:37

您可以添加此代码:

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

You can add this code:

google.maps.event.addListenerOnce(map, 'idle', function() {
    google.maps.event.trigger(map, 'resize');
});
风透绣罗衣 2024-11-25 09:18:37

问题是,当您尝试使用 style.visibility ='hidden' 初始化 DIV 标记内的 Google 地图

来解决此问题时,您必须在可见 DIV 内声明 map_canvas div,例如:

<div id="MapWindow"> 
    <div id="map_canvas" style="width:390px; height:390px; border-color: black"></div>
</div>

现在,如果您使用 jQuery ,在 $(function()) 中,您应该将“MapWindow”可见性设置为隐藏(如果您希望 GMap 开始隐藏)。

<script type="text/javascript">

var map;
var marker;

$(function () {
    initGoogleMap();
    document.getElementById("MapWindow").style.visibility = 'hidden';
});

function initGoogleMap() {
    var latlng = new google.maps.LatLng(-30, -60);
    var options = { zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP};
    map = new google.maps.Map(document.getElementById("map_canvas"), options);
    marker = new google.maps.Marker({
        draggable: false,
        position: latlng,
        map: map,
        title: ''
    });
}

function OpenPopupWindow()
    document.getElementById("MapWindow").style.visibility = 'visible';
    $('#MapWindow').data('tWindow').center().open();        
}

</script>

最后,当您想要显示 DIV(或弹出窗口,例如)时,在显示之前,您必须将 DIV 可见性设置为“visible”

;)

The problem is when you try to initialize a Google Map inside a DIV tag with style.visibility ='hidden'

to solve this, you have to declare the map_canvas div inside a visible DIV, for example:

<div id="MapWindow"> 
    <div id="map_canvas" style="width:390px; height:390px; border-color: black"></div>
</div>

now, if you're using jQuery, in $(function()) you should set 'MapWindow' visibility in hidden (if you want that GMap starts hidden).

<script type="text/javascript">

var map;
var marker;

$(function () {
    initGoogleMap();
    document.getElementById("MapWindow").style.visibility = 'hidden';
});

function initGoogleMap() {
    var latlng = new google.maps.LatLng(-30, -60);
    var options = { zoom: 14, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP};
    map = new google.maps.Map(document.getElementById("map_canvas"), options);
    marker = new google.maps.Marker({
        draggable: false,
        position: latlng,
        map: map,
        title: ''
    });
}

function OpenPopupWindow()
    document.getElementById("MapWindow").style.visibility = 'visible';
    $('#MapWindow').data('tWindow').center().open();        
}

</script>

Finally, when you wan to show de DIV (or popup window, for example), before show it, you have to set the DIV visibility in 'visible'

;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文