为什么Infobox无法在Google Maps模式窗口内正确加载?

油桃

我有一个地图,上面有特定区域所有酒店的标记,当我单击每个标记时,会显示每个酒店的信息框。该地图将在模式窗口中打开。

当用户单击“查看更大的地图”按钮时,地图将显示在模式窗口中,默认情况下该特定酒店的信息框处于打开状态。但是,它没有正确加载的信息框,它似乎已损坏并且不在中央,当我单击标记时,它会正确显示它。

另外,当我关闭模式窗口并再次打开它时,地图不会完全渲染,因此我必须放大或缩小才能完全渲染它。任何想法?

我尝试过,google.maps.event.trigger(mapObject, "resize");但没有成功。

Java脚本

(function(A) {

if (!Array.prototype.forEach)
    A.forEach = A.forEach || function(action, that) {
        for (var i = 0, l = this.length; i < l; i++)
            if (i in this)
                action.call(that, this[i], i, this);
        };

    })(Array.prototype);

    var
    mapObject,
    markers = [],
    markersData = {
        'Single_hotel': [
        {
            name: 'Villas',
            location_latitude: 37.713490, 
            location_longitude: 20.980900,
            map_image_url: 'img/villas/280.jpg',
            name_point: 'Luxury Villas',
            description_point: 'Lorem Ipsum',
            get_directions_start_address: '',
            phone: '+30 2641 085625',
            url_point: 'single_hotel.html'
        },
        {
            name: 'Villas2',
            location_latitude: 37.881908, 
            location_longitude: 20.705990,
            map_image_url: 'img/villas/280.jpg',
            name_point: 'Luxury Villas 2',
            description_point: 'Lorem Ipsum',
            get_directions_start_address: '',
            phone: '+30 2641 085625',
            url_point: 'single_hotel.html'
        }

        ]

    };


        var mapOptions = {
            zoom: 14,
            center: new google.maps.LatLng(37.713490, 20.980900),
            mapTypeId: google.maps.MapTypeId.ROADMAP,

            mapTypeControl: false,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                position: google.maps.ControlPosition.LEFT_CENTER
            },
            panControl: false,
            panControlOptions: {
                position: google.maps.ControlPosition.TOP_RIGHT
            },
            zoomControl: true,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.LARGE,
                position: google.maps.ControlPosition.TOP_LEFT
            },
            scrollwheel: false,
            scaleControl: false,
            scaleControlOptions: {
                position: google.maps.ControlPosition.TOP_LEFT
            },
            streetViewControl: true,
            streetViewControlOptions: {
                position: google.maps.ControlPosition.LEFT_TOP
            },

        };
        var marker;
        mapObject = new google.maps.Map(document.getElementById('map_modal'), mapOptions);
        for (var key in markersData)
            markersData[key].forEach(function (item) {
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(item.location_latitude, item.location_longitude),
                    map: mapObject,
                    icon: 'img/pins/' + key + '.png',
                });


                if ('undefined' === typeof markers[key])
                    markers[key] = [];
                    markers[key].push(marker);

                google.maps.event.addListener(marker, 'click', (function () {
                  closeInfoBox();
                  getInfoBox(item).open(mapObject, this);
                  mapObject.setCenter(new google.maps.LatLng(item.location_latitude, item.location_longitude));
 }));

});

    function onHtmlClick(key,key2) {
      google.maps.event.trigger(markers[key][key2], 'click');

    }

    function hideAllMarkers () {
        for (var key in markers)
            markers[key].forEach(function (marker) {
                marker.setMap(null);
            });
    };

    function closeInfoBox() {
        $('div.infoBox').remove();
    };

    function getInfoBox(item) {
        return new InfoBox({
            content:
            '<div class="marker_info" id="marker_info">' +
            '<img src="' + item.map_image_url + '" alt="Image"/>' +
            '<h3>'+ item.name_point +'</h3>' +
            '<span>'+ item.description_point +'</span>' +
            '<div class="marker_tools">' +
            '<form action="http://maps.google.com/maps" method="get" target="_blank" style="display:inline-block""><input name="saddr" value="'+ item.get_directions_start_address +'" type="hidden"><input type="hidden" name="daddr" value="'+ item.location_latitude +',' +item.location_longitude +'"><button type="submit" value="Get directions" class="btn_infobox_get_directions">Directions</button></form>' +
                '<a href="tel://'+ item.phone +'" class="btn_infobox_phone">'+ item.phone +'</a>' +
                '</div>' +
                '<a href="'+ item.url_point + '" class="btn_infobox">Details</a>' +
            '</div>',
            disableAutoPan: false,
            maxWidth: 0,
            pixelOffset: new google.maps.Size(10, 125),
            closeBoxMargin: '5px -20px 2px 2px',
            closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
            isHidden: false,
            alignBottom: true,
            pane: 'floatPane',
            enableEventPropagation: true
        });


    };

HTML代码

 <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<!-- Button to Open the Modal -->
<a href="#" class="btn_map location_map_btn properties" data-toggle="modal" data-target="#myModal" onclick="onHtmlClick('Single_hotel',0)">View Larger Map (Single_hotel[0])<i class="icon-location-5"></i></a><br>
<a href="#" class="btn_map location_map_btn properties" data-toggle="modal" data-target="#myModal" onclick="onHtmlClick('Single_hotel',1)">View Larger Map (Single_hotel[1])<i class="icon-location-5"></i></a>


<!-- The Modal map-->

<div class="modal fade" id="myModal">
  <div class="modal-dialog">
    <div class="modal-content">

      <!-- Modal Header -->
      <div class="modal-header">
        <h4 class="modal-title">Search By Map</h4>
        <button type="button" class="close" data-dismiss="modal">&times;</button>
      </div>

      <!-- Modal body -->
      <div class="modal-body">
        <div id="map_modal" class="map"></div>
      </div>

      <!-- Modal footer -->
      <div class="modal-footer">
        <button type="button" class="btn_map location_map_btn properties" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>
<!-- End modal map -->

<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script src="https://cdn.jsdelivr.net/gh/googlemaps/v3-utility-library@master/infobox/src/infobox.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

CSS代码

#map {
  height: 100%;
}


html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.map {
  height: 400px;
  width: 500px;
}

请参阅jsfiddle

埃文

模态仍在加载时,似乎单击了标记,这导致信息框无法在模态内正确呈现。在显示信息框之前,请尝试等待模式加载完毕。见下文:

<a href="#" class="btn_map location_map_btn properties" data-toggle="modal" data-target="#myModal">View Larger Map (Single_hotel[0])<i class="icon-location-5"></i></a><br>

$('#myModal').on('shown.bs.modal', function(event) {
    onHtmlClick('Single_hotel', 0);
});

希望这可以帮助!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

多个窗口的Tkinter示例代码,为什么按钮无法正确加载?

多个窗口的Tkinter示例代码,为什么按钮无法正确加载?

Google Maps:InfoWindow,InfoBox,InfoBubble

在模式弹出窗口内交换时无法定位元素

单击信息窗口内的按钮后更改Google Maps标记属性

为什么我的Google字体无法通过@ font-face加载?

SweetAlert模态窗口内的Google reCaptcha

为什么Google Cloud Storage无法发送正确的缓存头?

为什么Google Optimize无法通过Google Analytics(分析)“要求”加载?

Google Maps中的信息窗口无法加载

为什么mapbox无法正确加载地图?

为什么我的视图无法正确加载?(Django)

为什么即使代码正确也无法加载我的图像?

Google Maps API无法正确加载

为什么我的简单Google Maps演示无法在JSFiddle上运行?

为什么Google Maps V2无法显示在模拟器中?

为什么Google表格无法从Maps API中找到reverseGeocode方法?

为什么Google Maps SDK无法在iPhone 6 iOS 8上运行?

为什么使用Google Maps API无法在iOS中加载Google Map?

为什么RichMarker库中的Google Maps的zIndex无法正常工作?

为什么我的Google Maps事件监听器无法正常工作?

为什么++无法正确递增?

Windows无法在某些窗口内单击

为什么Google Maps api的python代码无法突然工作

为什么Google Maps API密钥无法识别我的引荐来源网址?

为什么文档高度等于窗口内部高度+ pageYoffset?

新窗口将无法正确加载

为什么我的代码无法正确加载?

无法在窗口内呈现 ContentControl