douxiaochun4964 2014-09-01 07:39
浏览 45
已采纳

谷歌地图Infowindow没有正确显示,但当我打开冷杉时,它运作良好

The infowindow is not showing properly on my map when clicking on a marker. The website is here.

You'll also notice the map control isn't properly displayed either.

var map;
var locations = <?php print json_encode(di_get_locations()); ?> ;
var markers = []
jQuery(function ($) {
    var options = {
        center: new google.maps.LatLng(51.840639771473, 5.8587418730469),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), options);

    for (var i = 0; i < locations.length; i++) {
        makeMarker(locations[i]);
    }
    centerMap();

});

function makeMarker(location) {
    var markerOptions = {
        map: map,
        position: new google.maps.LatLng(location.lat, location.lng)
    };
    var marker = new google.maps.Marker(markerOptions);
    markers.push(marker);
    var content = '';

    var infowindow = new google.maps.InfoWindow({
        content: "test",
        size: new google.maps.Size(50, 50),
        disableAutoPan: true
    });


    google.maps.event.addListener(marker, 'click', function (e) {
        infowindow.open(map, marker);
    });
}

function centerMap() {
    map.setCenter(markers[markers.length - 1].getPosition());
}
  • 写回答

2条回答 默认 最新

  • duanjiao8871 2014-09-01 07:51
    关注

    had solved this problem before. Google Map will not display properly when you load the map into a "display:none" canvas. Bootstrap modal win will be hidden in the beginning. So you have to make sure that map canvas can't be hidden before Google Map finished loading.

    My work around is move the canvas to a positon which user can't see it and load map into it. After a few second (maybe 500ms), move map to the right position (in your case is modal body).

    Javascipt:

    $(function(){
        var mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(-34.397, 150.644),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById("map"), mapOptions);
        setTimeout(function(){
            $('.modal-body').append($("#map").css("margin-top","0px").get(0));
        },500);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器