Infowindows不会使用Google Maps API显示

时间:2015-08-03 09:46:03

标签: google-maps google-maps-api-3

我已经阅读了有关“Infowindows未显示”的所有关于stackoverflow的帖子。在创建信息窗或任何意外声明之后创建地图不是问题。

地图显示标记,它可以计算路线,但是当您点击标记时,它不会显示任何信息窗口。这是我制作的整个程序的一小部分,但我已经使它可以重现,以便您更好地阅读它。 (API-Key未显示)

代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="styles.css" media="screen" />
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API-KEY&v=3.20&signed_in=false&libraries=places">    </script> 
    <title>Try</title>

    <script>


  function initialize() {

    var map = new google.maps.Map(document.getElementById('map-canvas'), {
      center: new google.maps.LatLng(40, -1),


     zoom: 11,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var carImage = 'car.png';
    var carLatLng = [];
    carLatLng[0] = new google.maps.LatLng(40, -1);

    var carMarker = [];
    for (var j = 0; j < carLatLng.length; j++) {
      var carMarkerPlotter = function(carMarker){
          carMarker = new google.maps.Marker({
            position: carLatLng[j],
            map: map,
            icon: carImage,
            clickable: true
          });
      }(carMarker);
    }


    var input = /** @type {HTMLInputElement} */(
        document.getElementById('pac-input'));
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    var searchBox = new google.maps.places.SearchBox(
      /** @type {HTMLInputElement} */(input));

    google.maps.event.addListener(searchBox, 'places_changed', function() {
      var places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      for (var i = 0, place; place = places[i]; i++) {

        var rendererOptions = [];
        var directionsDisplay = [];

        for (var k = 0; k < carLatLng.length; k++) {

            rendererOptions[k] = { map: map };
            directionsDisplay[k] = new google.maps.DirectionsRenderer(rendererOptions[k]);

            var request = {
                  origin: carLatLng[k],
                  destination: place.geometry.location,
                  travelMode: google.maps.DirectionsTravelMode.DRIVING
                };

            directionsService = new google.maps.DirectionsService();
            var dir = function(directionsDisplay) {
              directionsService.route(request, function (response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(response);
                } else alert('Failed to get directions');
              });
            }(directionsDisplay[k]);
          }

        }
      });

      var infowindow1 = new google.maps.InfoWindow({
         content: "Try"
     });

      google.maps.event.addListener(carMarker[0], 'click', function() {
        infowindow1.open(map,carMarker[0])
      });


    }

    google.maps.event.addDomListener(window, 'load', initialize);

    </script>

  </head>

  <body>
    <input id="pac-input" class="controls" type="text" placeholder="Search">
    <div id="map-canvas"></div>
  </body>
</html>

感谢您的帮助。我遇到这个问题已经好几天了。

编辑:

  • 在Firefox的控制台中,它会输出“TypeError:a is undefined”。在main.js:17:1612

  • 在Chromium的控制台中,它打印出“Uncaught TypeError:无法读取未定义的属性'__e3_'”。在main.js:17

  • JSFiddle无法正常工作(没有找到错误,这是我第一次使用它):https://jsfiddle.net/oamjue5p/2/

1 个答案:

答案 0 :(得分:0)

正如geocodezip所说,

  

carMarker是google.maps.Marker而不是数组,

     

未捕获的TypeError:google.maps.event.addListeners不是函数