te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - google map appear with grey inside modal in bootstrap - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - google map appear with grey inside modal in bootstrap - Stack Overflow

programmeradmin3浏览0评论

I use "Bootstrap v3.3.5 () Copyright 2011-2015 Twitter, Inc. " for my site and i want to add google-map. I run this code, and all maps is grey. I dont understand why this not work with modal. Can someone help me?

I also have included <script src=";></script>.

HTML:

<body>
  <a class="openmodal" href="#contact" data-toggle="modal" data-id="Peggy Guggenheim Collection - Venice">Contact</a>
  <div class="modal fade" id="contact" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content" id="back">
        <div class="modal-header">
        <h4>Contact<h4>
      </div>
      <div class="modal-body">
        <div id="map"></div>
      </div>
      <div class="modal-footer">
        <a class="btn btn-default" data-dismiss="modal">Close</a>
      </div>
  </div>
</div>

JS:

<script src=";></script>
<script>
  function initialize() {
    var mapCanvas = document.getElementById('map');
    var mapOptions = {
      center: new google.maps.LatLng(44.5403, -78.5463),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

CSS:

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

I use "Bootstrap v3.3.5 (http://getbootstrap.) Copyright 2011-2015 Twitter, Inc. " for my site and i want to add google-map. I run this code, and all maps is grey. I dont understand why this not work with modal. Can someone help me?

I also have included <script src="https://maps.googleapis./maps/api/js"></script>.

HTML:

<body>
  <a class="openmodal" href="#contact" data-toggle="modal" data-id="Peggy Guggenheim Collection - Venice">Contact</a>
  <div class="modal fade" id="contact" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content" id="back">
        <div class="modal-header">
        <h4>Contact<h4>
      </div>
      <div class="modal-body">
        <div id="map"></div>
      </div>
      <div class="modal-footer">
        <a class="btn btn-default" data-dismiss="modal">Close</a>
      </div>
  </div>
</div>

JS:

<script src="https://maps.googleapis./maps/api/js"></script>
<script>
  function initialize() {
    var mapCanvas = document.getElementById('map');
    var mapOptions = {
      center: new google.maps.LatLng(44.5403, -78.5463),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

CSS:

#map {
  width: 500px;
  height: 400px;
}
Share Improve this question edited May 4, 2021 at 22:25 Dan Atkinson 11.7k14 gold badges88 silver badges116 bronze badges asked Aug 26, 2015 at 1:21 GomuGomuNoRocketGomuGomuNoRocket 8393 gold badges15 silver badges39 bronze badges 4
  • Adding the version of bootstrap you are using might help the other find your answer – dvhh Commented Aug 26, 2015 at 1:36
  • i found this similar "stackoverflow./questions/11742839/…". the solution was adding $("#contact-modal").on("shown.bs.modal", function () { google.maps.event.trigger(map, "resize"); }); but how and where added this? – GomuGomuNoRocket Commented Aug 26, 2015 at 2:02
  • can you create a fiddle please – Amit singh Commented Aug 26, 2015 at 6:54
  • use it inside: $(document).ready(function () {}); – Azmeer Commented Feb 21, 2017 at 17:41
Add a ment  | 

3 Answers 3

Reset to default 13

Here is the code above in a working fiddle -> http://jsfiddle/wgur1z7n/ (bootstrap 3.3.5)

Trigger the google maps resize event after the modal is shown :

$('#contact').on('shown.bs.modal', function () {
    google.maps.event.trigger(map, "resize");
});

Triggering "resize" did not work for me. The map showed up, but with wrong coordinates and wrong zoom level. You probably have an initMap function on your page. This worked for me:

<script>
    $('#myModal').on('shown.bs.modal', function () {
        initMap();
    });
</script>

Try this rookie solution

<!DOCTYPE html>
<html>

<head>
    <title>Place Autoplete</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=yes">
    <meta charset="utf-8">
    <style>
    #map {
        height: 450px;
        width: 1080px;
    }

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

    .controls {
        margin-top: 10px;
        border: 1px solid transparent;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        height: 32px;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    }

    #pac-input {
        background-color: #fff;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        margin-left: 12px;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 300px;
    }

    #pac-input:focus {
        border-color: #4d90fe;
    }

    .pac-container {
        font-family: Roboto;
    }

    #type-selector {
        color: #fff;
        background-color: #4d90fe;
        padding: 5px 11px 0px 11px;
    }

    #type-selector label {
        font-family: Roboto;
        font-size: 13px;
        font-weight: 300;
    }

    .modal {
        display: block;

        z-index: 1;
        padding-top: 20px;
        padding-bottom: 20px width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgb(0, 0, 0);
        background-color: rgba(0, 0, 0, 0.4);
    }

    .modal-content {

        background-color: #fefefe;
        margin-left: 60px;
        margin-bottom: 20px;
        padding: 0;
        border: 1px solid #888;
        width: 90%;
        height: 90%;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        -webkit-animation-name: animatetop;
        -webkit-animation-duration: 4s;
        animation-name: animatetop;
        animation-duration: 4s
    }

    @-webkit-keyframes animatetop {
        from {
            top: -900px;
            opacity: 0
        }
        to {
            top: 0;
            opacity: 1
        }
    }

    @keyframes animatetop {
        from {
            top: -900px;
            opacity: 0
        }
        to {
            top: 0;
            opacity: 1
        }
    }

    .close {
        color: white;
        float: right;
        font-size: 40px;
        font-weight: bold;
        margin-top: 10px;
    }

    .close:hover,
    .close:focus {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }

    .modal-header {
        padding: 2px 16px;
        background-color: #a8c9ff;
        color: white;
    }

    .modal-body {
        padding: 2px 16px;
        background-color: white;
        color: black;
    }

    .modal-footer {
        padding: 2px 16px;
        background-color: #a8c9ff;
        color: white;
    }
    </style>
</head>

<body onload="clr()">
    <div>
        <div id='myModal' class='modal'>
            <div class='modal-content'>
                <div class='modal-header'>
                    <span onclick='cl()' class='close' style="border: 2px; border-color: black">OK</span>
                    <h2>Search location and click OK</h2>
                    <h4 style="color: red">Note : If map dosen't load propery Click <a href="javascript:;" onclick="ref()">here</a>!   (Click on location to select or Drag marker to position.)</h4></div>
                <div class='modal-body'>
                    <input id="pac-input" class="controls" type="text" placeholder="Enter a location">
                    <div id="type-selector" class="controls">
                        <input type="radio" name="type" id="changetype-all">
                        <label for="changetype-all">All</label>
                        <input type="radio" name="type" id="changetype-establishment">
                        <label for="changetype-establishment">Establishments</label>
                        <input type="radio" name="type" id="changetype-address">
                        <label for="changetype-address">Addresses</label>
                        <input type="radio" name="type" id="changetype-geocode" checked="checked">
                        <label for="changetype-geocode">Geocodes</label>
                    </div>
                    <div id="map" style="position: none"></div>
                </div>
                <div class='modal-footer'></div>
            </div>
        </div>
        <div style="">
            <input type="button" onclick="notifi()" id="btn" value="Show map">
            <input type="text" style="width: 400px" id="Location" placeholder="Choose location from map..." disabled="">
        </div>
    </div>
    <script>
    function initMap() {
        var $Location = document.getElementById('Location');

        var map = new google.maps.Map(document.getElementById('map'), {
            center: { lat: -33.8688, lng: 151.2195 },
            zoom: 13
        });
        google.maps.event.trigger(map, 'resize');
        var input = /** @type {!HTMLInputElement} */ (
            document.getElementById('pac-input'));
        var types = document.getElementById('type-selector');
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
        map.controls[google.maps.ControlPosition.TOP_LEFT].push(types);
        var autoplete = new google.maps.places.Autoplete(input);
        autoplete.bindTo('bounds', map);
        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
            map: map,
            anchorPoint: new google.maps.Point(0, -29),
            draggable: true
        });
        autoplete.addListener('place_changed', function() {
            infowindow.close();
            marker.setVisible(false);
            var place = autoplete.getPlace();
            if (!place.geometry) {

                window.alert("No details available for input: '" + place.name + "'");
                return;
            }

            if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
            } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
            }
            marker.setIcon( /** @type {google.maps.Icon} */ ({
                url: place.icon,
                size: new google.maps.Size(71, 71),
                origin: new google.maps.Point(0, 0),
                anchor: new google.maps.Point(17, 34),
                scaledSize: new google.maps.Size(35, 35)
            }));
            marker.setPosition(place.geometry.location);
            marker.setVisible(true);
            $Location.value = "Latitude : " + place.geometry.location.lat() + "  Longitude : " + place.geometry.location.lng();
            google.maps.event.addListener(marker, 'dragend', function(marker) {
                var latLng = marker.latLng;
                $Location.value = "Latitude : " + latLng.lat() + "  Longitude : " + latLng.lng();

            });
            google.maps.event.addListener(marker, 'click', function(event) {
                $Location.value = "Latitude : " + this.getPosition().lat() + "  Longitude : " + this.getPosition().lng();
            });
            var address = '';
            if (place.address_ponents) {
                address = [
                    (place.address_ponents[0] && place.address_ponents[0].short_name || ''),
                    (place.address_ponents[1] && place.address_ponents[1].short_name || ''),
                    (place.address_ponents[2] && place.address_ponents[2].short_name || '')
                ].join(' ');
            }
            infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
            infowindow.open(map, marker);
        });


        map.addListener('click', function(e) {
            setTimeout(function() { marker.setPosition(e.latLng); }, 10);
            $Location.value = "Latitude : " + e.latLng.lat() + "  Longitude : " + e.latLng.lng();
            google.maps.event.addListener(marker, 'click', function(event) {
                $Location.value = "Latitude : " + this.getPosition().lat() + "  Longitude : " + this.getPosition().lng();
            });
        });
        marker.addListener('click', function() {

            map.setCenter(marker.getPosition());
        });

        function setupClickListener(id, types) {
            var radioButton = document.getElementById(id);
            radioButton.addEventListener('click', function() {
                autoplete.setTypes(types);
            });
        }
        setupClickListener('changetype-all', []);
        setupClickListener('changetype-address', ['address']);
        setupClickListener('changetype-establishment', ['establishment']);
        setupClickListener('changetype-geocode', ['geocode']);
    }
    </script>
    <script type="text/javascript">
    var modal = document.getElementById('myModal');
    var span = document.getElementsByClassName("close")[0];
    span.onclick = function() {
        modal.style.display = "none";
    }
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }

    function cl() {

        modal.style.display = "none";
    }

    function notifi() {


        modal.style.display = "block";
        clr();
    }

    function explode() {
        modal.style.display = "none";
    }
    setTimeout(explode, 3000);

    function ref() {
        location.reload();
    }

    function clr() {
        document.getElementById("Location").value = "";
        document.getElementById("pac-input").value = "";
    }
    </script>
    <script src="https://maps.googleapis./maps/api/js?key=AIzaSyBPQ7PC_2JN_9jNw4z3N-PIsOtJF6HY-Hs&libraries=places&callback=initMap" async defer></script>
</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论