繁体   English   中英

Google Maps API:markerwithlabel.js - 未捕获的ReferenceError:未定义google

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

我已经阅读了文档和示例,但是当我尝试包含markerwithlabel.js文件时,我似乎无法解决初始化错误(“Uncaught ReferenceError:google未定义”+未捕获的ReferenceError:homeLatLng未定义),它让我想起“在地图完成之前你无法加载东西”概率。

我能做什么?

尝试了什么:

<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>

..

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM