未捕获的类型错误:无法读取未定义的 Google 地图的属性“应用”

Lew*_*wis 4 javascript google-maps

我正在使用谷歌地图在地图上制作标记,我正在尝试使用以下代码将地址转换为地理编码:

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

<script type="text/javascript">
var map;

 function initialize() {
   var chamberLocation = {lat: 43, lng: -82};
   var geocoder = new google.maps.Geocoder();
   map = new google.maps.Map(document.getElementById('map'), {
     center: {lat: 42.9745, lng: -82.4066},
     zoom: 14,
     styles: [{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","stylers":[{"hue":149},{"saturation":-78},{"lightness":0}]},{"featureType":"road.highway","stylers":[{"hue":-31},{"saturation":-40},{"lightness":2.8}]},{"featureType":"poi","elementType":"label","stylers":[{"visibility":"off"}]},{"featureType":"landscape","stylers":[{"hue":163},{"saturation":-26},{"lightness":-1.1}]},{"featureType":"transit","stylers":[{"visibility":"off"}]},{"featureType":"water","stylers":[{"hue":3},{"saturation":-24.24},{"lightness":-38.57}]}],
     zoomControl: false,
     scaleControl: false,
     mapTypeControl: false,
     disableDefaultUI: false,
     streetViewControl: false,
     rotateControl: false,
     scrollwheel: false,
     draggable: false
   });
   codeAddress(geocoder, map);

   }

   function codeAddress(geocoder, map) {
       var address = 'place';
       geocoder.geocode({ 'address' : address }), function(results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
               var marker = new google.maps.Marker({
                   map: map,
                   position: results[0].geometry.location
               });
           } else {
               console.log('This didnt work' + status);
           }
       };
   }


</script>
Run Code Online (Sandbox Code Playgroud)

每当我这样做时,我都会收到一条错误消息 Uncaught TypeError: Cannot read property 'apply' of undefined

是什么导致了这个错误?我不确定如何解决这个问题。我是否必须导入另一个谷歌地图 API?

Jon*_*eal 6

该错误是一个错字。我把代码放在下面,并//change在错字所在的地方进行了评论。geocoder.geocode({}, callback)是一个接受对象和回调的函数,但是你有geocoder.geocode({ 'address' : address }),错字是)它应该是geocoder.geocode({ 'address' : address }, function(results, status) { ...

<div id="map" style="width: 320px; height: 480px;"></div>

  <script type="text/javascript">
    var map;

    function initialize() {
      // your code 
      // etc ... 
      codeAddress(geocoder, map);
    }

    function codeAddress(geocoder, map) {
      var address = 'place';
      geocoder.geocode({
          'address': address
        }, // change
        function(results, status) {
          if (status == 'OK') { // change

            var marker = new google.maps.Marker({
              map: map,
              position: results[0].geometry.location
            });

            // some debug output
            console.log("status is: " + status)
            console.log("results is: " + JSON.stringify(results[0].geometry.location))
          } else {
            console.log('This didnt work' + status);
          }
        });
    };
  </script>

  <script async defer src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initialize"></script>
Run Code Online (Sandbox Code Playgroud)