Uncaught InvalidValueError: initMap is not a function

前端 未结 6 1782
半阙折子戏
半阙折子戏 2020-12-10 16:57

I am trying to load a google map with some specific parmeters. I understand the problem is most likely that the initMap function needs to be declared globally. However, I h

相关标签:
6条回答
  • 2020-12-10 17:00

    Not a best way but it fixed my problem.

    Call initMap() function manually as a fallback if ever the callback function wont work.

    jQuery(document).ready(function($) {
        initMap();
    });
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-10 17:04

    I copied your code but there are some syntax error. So i can't test. But your error is about initMap (of course :)). Delete all code and check initMap and your window.initMap.

    function initMap() {alert("ok");}
    

    Is google callback works?

    0 讨论(0)
  • 2020-12-10 17:04
    async="false" 
    

    would work, try it!

    0 讨论(0)
  • 2020-12-10 17:09

    In my case the function was async and it was crashing since the initmap was being called but it was not loaded at that point yet. So you can just write async="false" as mentioned before:

    <script async="false" type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<YOUR KEY>-39Fxo&libraries=places&callback=initMap"></script>
    
    0 讨论(0)
  • 2020-12-10 17:24

    Anyone having a similar issue, initializing the map variable outside the initMap function worked for me

    <script>
      var map;
    
      function initMap(){
    
          //your code here
      }
    </script>
    
    0 讨论(0)
  • 2020-12-10 17:26

    Found it... Though very late to the party...

    If you downloaded the google maps js code from this link https://maps.googleapis.com/maps/api/js and then tried calling it using

    <script async defer
      src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
    

    There is bug in the development tools that the code gets modified.

    Find the initMap in the downloaded file and replace whole file with "" Or replace with a fresh copy form the above link and then lock the file or mark it as ReadOnly to avoid the same in future.

    0 讨论(0)
提交回复
热议问题