Google Map Event Listener似乎不起作用 [英] Google Map Event Listener does not seem to work

查看:116
本文介绍了Google Map Event Listener似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用地方地图搜索位置,并且当用户单击标记时,我创建了一个侦听器来缩放地图,但似乎不起作用.

I have used the places map to search for a location and when user click on the marker i have created a Listener to zoom the map, but does not seem to work.

我尝试过

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

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

It did not seem to work, any suggestions?

推荐答案

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

You are creating several markers in for loop of event listener google.maps.event.addListener(searchBox, 'places_changed', function() {...}. But event listener for marker is set outside of it.

应在创建标记后将其移到位置:

It should be moved to place after marker creation:

        // 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);
    }

这篇关于Google Map Event Listener似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆