【问题标题】:Google Maps API V3 : event listener on a classGoogle Maps API V3:类上的事件监听器
【发布时间】:2015-11-23 05:59:30
【问题描述】:

可以使用 Google Maps V3 API 检测事件,例如(例如 1):

 google.maps.event.addListener('drag', function(event) { 
            $("#latD").val(event.latLng.lat()); 
            $("#longD").val(event.latLng.lng()); 
        }); 

或(例如 2)

google.maps.event.addListener(markerDep, 'drag', function(event) { 
        $("#latD").val(event.latLng.lat()); 
        $("#longD").val(event.latLng.lng()); 
    }); 

在这里,我指定了一个标记的实例。

根据 Google Maps API 规范,可以为特定事件创建侦听器(示例 1),甚至可以为特定实例上的特定事件创建侦听器(示例 2)。

但是是否有可能为同一个类的所有实例检测到相同的事件?例如,我可以在所有 Marker 实例上检测到“dragend”事件吗?

【问题讨论】:

  • 您可以参考文档中您从哪里获得这些示例吗?正如您在问题中所写的那样,它们对我来说都没有意义。
  • 我更新了上面的代码。我认为我的问题的答案是“不,你不能”。我可能必须为 Marker 的每个实例添加一个侦听器...
  • 现在的例子更有意义了,但我仍然不知道第一个例子来自哪里。所有MapsEventListener 函数都将实例作为第一个参数。
  • 它们来自我之前编写的代码,这是错误的,我在我的 2 个 Notepad++ 窗口之间犯了一个错误,对不起。是的,这就是我的想法,无论如何,谢谢。我必须创建为侦听器,因为存在 Marker 实例

标签: javascript google-maps-api-3 google-maps-markers dom-events


【解决方案1】:

如果您有多个标记并尝试为每个标记创建一个事件,在本例中为 dragend 事件,您需要将其包含在生成标记的 for 循环中。

下面的 sn-p 假设您在拖动标记后尝试获取标记的经度和纬度。这些值被插入到地图下方的输入框中。

#map {
  height: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="map"></div>

<input id="latD" type="text">
<input id="longD" type="text">

<script>
// The following map creates complex markers to indicate destinations
// Note that the anchor is set to (0,32) to correspond to the base of the iamge.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 2,
    scrollwheel: false,
    navigationControl: false,
    mapTypeControl: false,
    scaleControl: false,
    draggable: true,
    center: {lat: -2.75181, lng: 24.11768}
  });

    setMarkers(map);
  }

  // Data for the markers consisting of a name, a LatLng and a zIndex for the
  // order in which these markers should display on top of each other
  var destinations = [
    ['Congo', -2.72760, 23.52084, 1],
    ['South Africa', -33.58880, 20.07114, 1],
    ['Brazil', -6.26352, -57.38128, 1]
  ];

  function setMarkers(map) {
    // Adds markers to the map.

    // Marker sizes are expressed as a Size of X,Y where the origin of the image
    // (0,0) is located in the top left of the image.

    // Origins, anchor positions and coordinates of the marker increase in the X
    // direction to the right and in the Y direction down.
    var image = {
      url: 'http://i.imgur.com/OG4CZt3.png',
      // This marker is 32 pixels wide by 32 pixels high.
      size: new google.maps.Size(32, 32),
      // The origin for this image is (0, 0).
      origin: new google.maps.Point(0, 0),
      // The anchor for this image is just off centre (0, 10).
      anchor: new google.maps.Point(0, 10)
    };
    // Shapes define the clickable region of the icon. The type defines an HTML
    // <area> element 'poly' which traces out a polygon as a series of X,Y points.
    // The final coordinate closes the poly by connecting to the first coordinate.
    var shape = {
      coords: [1, 1, 1, 32, 30, 32, 30, 1],
      type: 'poly'
    };
    
    for (var i = 0; i < destinations.length; i++) {
      var destination = destinations[i];
      var marker = new google.maps.Marker({
          position: {lat: destination[1], lng: destination[2]},
          map: map,
          draggable: true,
          icon: image,
          shape: shape,
          animation: google.maps.Animation.DROP,
          title: destination[0],
          zIndex: destination[3],
      });

      marker.addListener('dragend', function() {
          jQuery("#latD").val(this.getPosition().lat()); 
          jQuery("#longD").val(this.getPosition().lng()); 
      });
    }
}
</script>

<script async defer
    src="https://maps.googleapis.com/maps/api/js?signed_in=false&callback=initMap"></script>

更多信息 in the docs

【讨论】:

    猜你喜欢
    • 2010-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多