在第0行加载gmaps消息时出现以下错误:未捕获(承诺中)InvalidValueError:initMap不是函数)

2024-06-28 11:20:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我在加载gmaps时出现以下错误/我克隆了https://github.com/alexbagirov/py-flightradar24

python3 -m trace --count -C . main.py

Message at line 0: Uncaught (in promise) InvalidValueError: initMap is not a function
Message at line 24: Uncaught TypeError: Cannot read property 'addListener' of undefined
Message at line 115: Uncaught TypeError: Cannot read property 'panTo' of undefined

  <!DOCTYPE html>
        <html>
          <head>
            <meta name="viewport" content="initial-scale=1.0">
            <meta charset="utf-8">
            <style>
              #map {
                height: 100%;
              }
              html, body {
                height: 100%;
                margin: 0;
                padding: 0;
              }
            </style>
          </head>
          <body>
            <div id="map"></div>
            <script src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap&libraries=geometry"
            async defer></script>
            <script src="qrc:///qtwebchannel/qwebchannel.js"></script>
            <script src="map.js"></script>
          </body>
        </html>
    
    
    
    
        var map;
    var aircrafts = {};
    
    class Aircraft {
        constructor(marker, speed, track) {
            this.marker = marker;
            this.speed = speed;
            this.track = track;
        }
    
        move(newPoint) {
            this.marker.setPosition(newPoint);
        }
    
        hide() {
            this.marker.setMap(null);
        }
    }
    
    window.onload = function() {
        new QWebChannel(qt.webChannelTransport, function (channel) {
            window.loader = channel.objects.aircraftManager;
    
            map.addListener('tilesloaded', function() {
                window.loader.paintAircrafts();
            });
    
            window.loader.startService();
        });
    }
    
    /* function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
            center: {lat: 56.8454603, lng: 60.6424976},
            zoom: 8,
            disableDefaultUI: true,
            clickableIcons: false,
            zoomControl: true
        });
    } */
    
    window.onload = function initMap() {
      map = new google.maps.Map(document.getElementById("map"), {
        center: { lat: 27.691163902, lng: 85.355331912 },
        // ktm airport cordinates
        zoom: 12,
        disableDefaultUI: true,
        clickableIcons: true,
        zoomControl: true,
    
        //panControl: true,
        mapTypeControl: true,
        //scaleControl: true,
        //streetViewControl: false,
        //overviewMapControl: false,
        //rotateControl: true,
      });
    };
    
    function addAircrafts(data) {
        let newAircrafts = JSON.parse(data);
        Object.keys(aircrafts).forEach(function(aircraftId, index) {
            if (aircraftId in newAircrafts) {
                var newAircraft = newAircrafts[aircraftId];
    
                aircrafts[aircraftId].track = newAircraft['track'];
                aircrafts[aircraftId].speed = newAircraft['speed'];
                aircrafts[aircraftId].marker.setIcon('icons/' + newAircraft['pic'] + '.png');
            }
            else {
                removeAircraft(aircraftId);
            }
        });
    
        Object.keys(newAircrafts).forEach(function(aircraftId, index) {
            if (!(aircraftId in aircrafts)) {
                let aircraft = newAircrafts[aircraftId];
                aircrafts[aircraftId] = new Aircraft(
                    new google.maps.Marker({
                        position: {lat: aircraft['lat'], lng: aircraft['lon']},
                        map: map,
                        icon: 'icons/' + aircraft['pic'] + '.png'
                    }),
                    aircraft['speed'],
                    aircraft['track']
                );
                aircrafts[aircraftId].marker.addListener('click', function() {
                    window.loader.handleClick(aircraftId);
                });
            }
        });
    }
    
    function moveAircrafts() {
        Object.keys(aircrafts).forEach(function(key, index) {
            let newPoint = google.maps.geometry.spherical.computeOffset(
                aircrafts[key].marker.position,
                aircrafts[key].speed * 0.514444,
                aircrafts[key].track
            );
            aircrafts[key].move(newPoint);
        });
    }
    
    function removeAircraft(id) {
        aircrafts[id].hide();
        delete aircrafts[id];
    }
    
    function getBounds() {
        return map.getBounds();
    }
    
    function moveMap(lat, lon) {
        let center = new google.maps.LatLng(lat, lon);
        map.panTo(center);
    }
        

Tags: keytruemapnewscriptfunctiontrackwindow
1条回答
网友
1楼 · 发布于 2024-06-28 11:20:01

删除initMap定义之前的window.onload

window.onload = function initMap() {}应替换为function initMap() {}。并检查您的API密钥是否有效

相关问题 更多 >