简体   繁体   English

Google Maps API找不到$(document).ready中定义的回调

[英]Google maps API can't find a callback defined in $(document).ready

I have a problem with Jquery and Google Maps API. 我在使用Jquery和Google Maps API时遇到问题。

Scripts seem to be located correctly in html 脚本似乎在html中正确定位

<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/main.js"></script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.min.js"></script>

The problem is that init map is inside $(document).ready 问题是init映射位于$(document).ready内部

$(document).ready(function () {
    let url = "ws://localhost:61614/";
    let topic = "stomp.topic";
    let client;

    let map, trackers = {};

    $("#connect_button").click(function () {
        connect(url);
        return false;
    });

    $("#disconnect_button").click(function () {
        disconnect();
        return false;
    });

    function initMap() {
        let mapOptions = {
            zoom: 8,
            center: new google.maps.LatLng(30, 0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map"), mapOptions)

    }
}

How correctly initMap must be accessed ? 如何正确访问initMap?

I think you forgot to call initMap function. 我认为您忘记了调用initMap函数。

Try this - https://jsitor.com/227rClFCE , 试试这个-https://jsitor.com/227rClFCE

The callback in scripts path https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap will look for global method name initMap , however it is not defined globally and scoped inside document.ready scope so it won't execute. 脚本路径https://maps.googleapis.com/maps/api/js?key=KEY&callback=initMap的回调将查找全局方法名称initMap ,但是它不是全局定义的,而是在document.ready范围内定义的,因此它赢得了不执行。 Either call the method inside document.ready callback or else after add this method inside Window object by doing Window.initMap = initMap below initMap function. 要么在document.ready回调中调用该方法,要么在执行initMap函数下的initMap Window.initMap = initMap之后在Window对象中添加此方法。

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

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