未捕获的TypeError:无法读取属性' apply'未定义的Google地图

时间:2017-02-28 20:58:47

标签: javascript google-maps

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

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

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

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

2 个答案:

答案 0 :(得分:4)

错误是拼写错误。我把下面的代码放在//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>

答案 1 :(得分:0)

今天我在使用距离矩阵库时也遇到了同样的错误,我们需要做的只是使用这里提到的回调函数https://developers.google.com/maps/documentation/javascript/distancematrix

根据此文档https://developers.google.com/maps/documentation/javascript/examples/distance-matrix#maps_distance_matrix-typescript,当我遇到上述错误时,我正在使用承诺

var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = 'Greenwich, England';
var destinationA = 'Stockholm, Sweden';
var destinationB = new google.maps.LatLng(50.087692, 14.421150);

var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
  {
    origins: [origin1, origin2],
    destinations: [destinationA, destinationB],
    travelMode: 'DRIVING',
    transitOptions: TransitOptions,
    drivingOptions: DrivingOptions,
    unitSystem: UnitSystem,
    avoidHighways: Boolean,
    avoidTolls: Boolean,
  }, callback);

function callback(response, status) {
  // See Parsing the Results for
  // the basics of a callback function.
  console.log(status);
  console.log(response);
}