javascript - 谷歌地图事件监听器似乎不起作用

标签 javascript google-maps onclick onclicklistener

我已经使用 places map 搜索位置,当用户点击标记时,我创建了一个监听器来缩放 map ,但似乎不起作用。

我试过了

<script>
    function initialize() {

      var markers = [];
      var map = new google.maps.Map(document.getElementById('map-canvas'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP
      });

      var defaultBounds = new google.maps.LatLngBounds(
          new google.maps.LatLng(-33.8902, 151.1759),
          new google.maps.LatLng(-33.8474, 151.2631));
      map.fitBounds(defaultBounds);

      // Create the search box and link it to the UI element.
      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));

      // [START region_getplaces]
      // Listen for the event fired when the user selects an item from the
      // pick list. Retrieve the matching places for that item.
      google.maps.event.addListener(searchBox, 'places_changed', function() {
        var places = searchBox.getPlaces();

        for (var i = 0, marker; marker = markers[i]; i++) {
          marker.setMap(null);
        }

        // For each place, get the icon, place name, and location.
        markers = [];
        var bounds = new google.maps.LatLngBounds();
        for (var i = 0, place; place = places[i]; i++) {
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };

          // Create a marker for each place.
          var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
          });

          markers.push(marker);

          bounds.extend(place.geometry.location);
        }

        map.fitBounds(bounds);
      });
      // [END region_getplaces]

      // Bias the SearchBox results towards places that are within the bounds of the
      // current map's viewport.
      google.maps.event.addListener(map, 'bounds_changed', function() {
        var bounds = map.getBounds();
        searchBox.setBounds(bounds);
      });

      google.maps.event.addListener(marker, 'click', function () {
        map.setZoom(13);
        map.setCenter(marker.getPosition());
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

似乎没有用,有什么建议吗?

最佳答案

您正在事件监听器 google.maps.event.addListener(searchBox, 'places_changed', function() {...}for 循环中创建多个标记. 但是标记的事件监听器设置在它之外。

它应该在标记创建后移动到地方:

        // Create a marker for each place.
        var marker = new google.maps.Marker({
            map: map,
            icon: image,
            title: place.name,
            position: place.geometry.location
        });

        markers.push(marker);

        google.maps.event.addListener(marker, 'click', function () {
            map.setZoom(13);
            map.setCenter(marker.getPosition());
        });

        bounds.extend(place.geometry.location);
    }

关于javascript - 谷歌地图事件监听器似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21799572/

相关文章:

php - 从 file_get_contents 更改/删除 html

javascript - 用于选择带有类的输入字段的 jQuery 选择器

google-maps - 我需要捕获多边形下方 map 上的点击事件

javascript - 如何使用 javascript/jquery 以编程方式单击第一个 li 元素?

onclick - 如何将 OnClick 添加到 Jetpack Compose 中的 LazyColumn 文本?

javascript - jQuery 多次点击处理

javascript - 如何触发文本输入事件以绕过应用程序的验证?

javascript - 使用纯 javascript slider 拇指在输入 slider 上的位置

google-maps - 谷歌地图方向api考虑交通

android - 加载由 ViewPager 管理的 Android 谷歌地图 fragment