javascript Google Maps API:markerwithlabel.js - Uncaught ReferenceError: google is not defined

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32692791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:37:42  来源:igfitidea点击:

Google Maps API: markerwithlabel.js - Uncaught ReferenceError: google is not defined

javascriptgoogle-mapsgoogle-maps-api-3google-maps-markers

提问by maxxyoo

I have read the docs and examples, but I it seems I cannot solve the initialization error ("Uncaught ReferenceError: google is not defined" + Uncaught ReferenceError: homeLatLng is not defined) when trying to include markerwithlabel.js file and it's reminds me to the "you cannot load something before the map is done" prob.

我已阅读文档和示例,但我似乎无法解决尝试包含markerwithlabel.js 文件时出现的初始化错误(“Uncaught ReferenceError: google is not defined” + Uncaught ReferenceError: homeLatLng is not defined),它提醒我“在地图完成之前您无法加载某些内容”问题。

What can I do?

我能做什么?

What was tried:

尝试了什么:

<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: "5K",
                   labelAnchor: new google.maps.Point(22, 0),
                   labelClass: "labels", // the CSS class for the label
                   labelStyle: {opacity: 0.75}
            });
    }
</script>

..

..

回答by Dr.Molle

markerwithlabel.js requires a already loaded maps-API.

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

When you load the maps-API asynchronously(as you do in your code), there is no guarantee that the maps-API is loaded when markerwithlabel.js will be loaded.

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

solution: load the maps-API synchronously

解决方案:同步加载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: "5K",
                   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>