首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要时加载Google Maps javascript文件

需要时加载Google Maps javascript文件
EN

Stack Overflow用户
提问于 2017-09-20 17:43:25
回答 1查看 416关注 0票数 0

我有一个页面与一个按钮,以打开一个模式的地图。由于用户使用此函数的次数很少,因此我想知道是否可以仅在需要时加载Google Maps库(我更希望用户在这种情况下等待,而不是每次加载页面时都等待)。

这是我的模式:

代码语言:javascript
复制
<div class="modal" id="showBuildingsMaps" data-backdrop="static"
    data-keyboard="false">
    <div class="modal-dialog modal-lg">
       <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">Select building</h4>
            </div>
            <div class="modal-body">
                <div class="box-body" style="height: 80vh;">
                    <ng-map zoom="3" center="[{{selectedBuilding.latitude}}, {{selectedBuilding.longitude}}]" default-style="false"> 
                       <marker ng-repeat="m in markers" position="{{m.latitude}},{{m.longitude}}" title="{{m.name}}"
                                        on-click="buildingFromMaps(m)"></marker> </ng-map>
                </div>
            </div>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>

Google Maps有几个库:

代码语言:javascript
复制
<!-- Maps -->
<script
    src="https://maps.googleapis.com/maps/api/js?key={myKey}"
    type="text/javascript">
    </script>
<!-- is useful to avoid google not found exception before this script was loaded bedore google maps -->
<script defer
    th:src="@{/static/assets/plugins/angular-maps/ng-map.min.js}"
    type="text/javascript"></script>
<script
    src="https://rawgit.com/allenhwkim/angularjs-google-maps/master/testapp/scripts/markerclusterer.js"
    type="text/javascript"></script>
<script>
    MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ 
      = 'https://raw.githubusercontent.com/googlemaps/js-marker-clusterer/gh-pages/images/m';  //changed image path 
  </script>
EN

回答 1

Stack Overflow用户

发布于 2017-09-20 18:36:01

添加onclick到你的按钮

代码语言:javascript
复制
<button type="button" onclick="loadGoogleMaps()" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>

然后是这样的东西

代码语言:javascript
复制
<script>
function loadGoogleMaps() {
var mapScript = document.createElement('script');
mapScript.src = put your source here;

document.head.appendChild(script);
}
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46318600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档