【问题标题】:Google Maps API: markerwithlabel.js - Uncaught ReferenceError: google is not definedGoogle Maps API:markerwithlabel.js - 未捕获的 ReferenceError:未定义 google
【发布时间】:2015-12-18 00:14:02
【问题描述】:

我已阅读文档和示例,但在尝试包含 markerwithlabel.js 文件和 这让我想起了“在地图完成之前你不能加载一些东西”的问题。

我能做什么?

尝试了什么:

<head>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>
<script type="text/javascript"> 
    var map;
    function initMap() {

            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 14,
                center: {lat: 52.5200066, lng: 13.404954}
            });

            var marker1 = new MarkerWithLabel({
                   position: homeLatLng,
                   draggable: true,
                   raiseOnDrag: true,
                   map: map,
                   labelContent: "$425K",
                   labelAnchor: new google.maps.Point(22, 0),
                   labelClass: "labels", // the CSS class for the label
                   labelStyle: {opacity: 0.75}
            });
    }
</script>

..

【问题讨论】:

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


    【解决方案1】:

    markerwithlabel.js 需要一个已经加载的 maps-API。

    当您异步加载 maps-API 时(就像您在代码中所做的那样),无法保证在加载 markerwithlabel.js 时加载 maps-API。

    解决方案:同步加载 maps-API

    <script src="https://maps.googleapis.com/maps/api/js?v=3&key=mykey"></script>
    <script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerwithlabel/src/markerwithlabel.js"></script>
    <script type="text/javascript"> 
        var map;
        function initMap() {
    
                map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 14,
                    center: {lat: 52.5200066, lng: 13.404954}
                });
    
                var marker1 = new MarkerWithLabel({
                       position: homeLatLng,
                       draggable: true,
                       raiseOnDrag: true,
                       map: map,
                       labelContent: "$425K",
                       labelAnchor: new google.maps.Point(22, 0),
                       labelClass: "labels", // the CSS class for the label
                       labelStyle: {opacity: 0.75}
                });
        }
    google.maps.event.addDomListener(window, 'load', initMap);
    </script>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2020-10-04
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    相关资源
    最近更新 更多