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

时间:2019-07-18 12:28:32

标签: javascript jquery google-maps google-maps-api-3 bootstrap-modal

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

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

此外,当我关闭模式窗口并再次打开它时,地图无法完全渲染,因此我必须放大或缩小以完全渲染它。有想法吗?

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

JavaScript

(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

1 个答案:

答案 0 :(得分:0)

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

server.stream-request-body = 2

希望这会有所帮助!