按钮上的setTimeout事件单击不起作用

时间:2014-08-05 01:52:08

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

嘿伙计们,我试图将它点击到点击此按钮的位置,setTimeout功能将启动并在模式中显示谷歌地图。现在,每当我点击按钮时,模态就会卡住显示加载的gif。如何解决这个问题的任何想法将不胜感激! (我必须在加载时将地图放在计时器上,否则它只会加载到模态的左上角。)

按钮:

<p><a id ="hammondButton" data-toggle="modal" href="#HmapModal" class="btn btn-de
fault"><b>View Map &raquo;</b></a></p>

代码:

<!-- Hammond Modal -->
    <div class="modal fade" id="HmapModal" tabindex="-1" role="dialog" aria-labelledby="HmapModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header" align="center">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h2 class="modal-title">Hammond Location</h2>
                </div>
                <div class="modal-body">
                    <div id="loadingGif" style="position: fixed; margin-left: 14%; margin-top: 15%;" ><img src="~/Content/Images/LGIF.gif" /></div>
                    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
                    <div style="overflow:hidden;height:500px;width:578px;">
                        <div id="gmap_canvas" style="height:500px;width:578px;"></div>
                        <style>
                            #gmap_canvas img {
                                max-width: none !important;
                                background: none !important;
                            }
                        </style><a class="google-map-code" href="http://www.map-embed.com" id="get-map-data">www.map-embed.com</a>
                    </div>
                    <script type="text/javascript">
                        $("#hammondButton").button().click(function () {
                            setTimeout(function init_map() {
                                google.maps.event.trigger(map, 'resize');
                                var myOptions = {
                                    zoom: 13,
                                    center: new google.maps.LatLng(30.4900271, -90.4629746),
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                };
                                map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
                                marker = new google.maps.Marker({ map: map, position: new google.maps.LatLng(30.4900271, -90.4629746) });
                                google.maps.event.addListener(marker, "click", function() { infowindow.open(map, marker); });
                                infowindow.open(map, marker);
                            }, 2500);
                            google.maps.event.addDomListener(window, 'load', init_map);
                            google.maps.event.trigger(map, 'resize');
                        });
                    </script>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal" onclick="window.location.href=window.location.href">Close</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

2 个答案:

答案 0 :(得分:1)

在你的代码中,你没有给出谷歌API密钥。

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=TRUE_OR_FALSE"></script>

如果要使用<a>,请在java脚本中编写以下内容。

$("#hammondButton").button().click(function () {
 event.preventDefault();
}

或者, 只需使用按钮代替<a>.

<button id="HmapModal" data-toggle="modal" data-target="HmapModal"></button>

然后在java脚本中添加单击

$("#hammondButton").click(function () {
                            setTimeout(function init_map() {
                                google.maps.event.trigger(map, 'resize');
                                var myOptions = {
                                    zoom: 13,
                                    center: new google.maps.LatLng(30.4900271, -90.4629746),
                                    mapTypeId: google.maps.MapTypeId.ROADMAP
                                };
                                map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions);
                                marker = new google.maps.Marker({ map: map, position: new google.maps.LatLng(30.4900271, -90.4629746) });
                                google.maps.event.addListener(marker, "click", function() { infowindow.open(map, marker); });
                                infowindow.open(map, marker);
                            }, 2500);
                            google.maps.event.addDomListener(window, 'load', init_map);
                            google.maps.event.trigger(map, 'resize');
                        });

答案 1 :(得分:0)

我现在可以提供的只是一些清理过的代码,不知道它会带来多少,如果有的话,它会在这里得到它。

<script type="text/javascript">
    var loc = new google.maps.LatLng(30.4900271, -90.4629746),
        myOptions = {
            zoom: 13,
            center: loc,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        },
        map = new google.maps.Map(document.getElementById("gmap_canvas"), myOptions),
        marker = new google.maps.Marker({
            map: map,
            position: loc
        });

    function init_map() {
        $("#hammondButton").click(function () {
            google.maps.event.addListener(marker, "click", function() {
                infowindow.open(map, marker);
            });

            // is this necessary?
            google.maps.event.trigger(map, 'resize');
        });

    }

    window.addEventListener("load", init_map);
</script>