未捕获的TypeError:无法读取属性' places'未定义的

时间:2016-12-27 07:23:01

标签: javascript jquery google-api



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyAW2XFdc_uzmVX4RfNjVaJvdOERDYcihSc&sensor=false&libraries=places&language=en-AU"></script>

<script>
  var autocomplete = new google.maps.places.Autocomplete($("#institute_city")[0], {});
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    var place = autocomplete.getPlace();
    console.log(place.address_components);
  });
</script>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

像这样使用

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?key=AIzaSyAW2XFdc_uzmVX4RfNjVaJvdOERDYcihSc&sensor=false&libraries=places&language=en-AU"></script>

</head>
<body>
  <input id="institute_city" type="text" size="50">
  <script>
    var input = document.getElementById('institute_city');
    var autocomplete = new google.maps.places.Autocomplete(input);
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
      var place = autocomplete.getPlace();
      console.log(place.address_components);
    });
  </script>
</body>
</html>