【问题标题】:How to listen to property change of an object? [closed]如何监听对象的属性变化? [关闭]
【发布时间】:2015-02-12 06:28:20
【问题描述】:

我一般是谷歌地图 api 和 js 的新手。
我有一个包含 2 个圆形对象的项目,它们可以更改半径和中心坐标,每次其中一个属性更改时,我都想执行特定功能。
圆的半径随着控制器的按下而改变(setradius 方法),圆也是可拖动的。我不想在用户拖动圆圈时每帧都执行该函数(如果属性没有更改,可能会在 0.1 秒后检查,然后才执行)。
希望我没有把它弄得太复杂。

TL;DR:如何在每次圆的属性发生变化时执行一个函数?

【问题讨论】:

  • 既然您提到“控制器”,那么您在客户端使用什么框架?而且,您能否在您的问题中添加一些更改半径的代码示例?
  • 对于控制器,我使用谷歌地图中的内置控制器选项,从这里获得参考 (developers.google.com/maps/documentation/javascript/examples/…)。我用一个简单的函数改变半径,如下所示:function(circle) {circle.setRadius(circle.getRadius() * 1.1}; 这个函数使给定的圆增大 10%
  • 如果您总是从同一组函数中更改半径,那么只需创建一个名为 updateRadius() 的新函数并在您更改半径的函数中调用它。
  • 每次半径变化时要执行什么函数?你的代码是什么样的?请提供一个Minimal, Complete, Tested and Readable example 来演示您的代码的外观以及您尝试过的您认为应该可以使用的方法。
  • 我可以将想要的功能(我需要在圆的属性更改时执行的功能)添加到每个更改半径的控制器,但我仍然需要注意拖动圆 - 我如何执行圆圈被拖到新位置后的功能?

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


【解决方案1】:

Google Maps Javascript API v3 是基于事件的。 google.maps.Circle 具有在其任何属性更改时触发的事件。向您的圈子添加事件侦听器以侦听 radius_changed 事件。

    cityCircle = new google.maps.Circle(populationOptions);
    google.maps.event.addListener(cityCircle, 'radius_changed', radiusChanged);

工作代码 sn-p 基于example in the documentation:

// This example creates circles on the map, representing
// populations in North America.

// First, create an object containing LatLng and population for each city.
var citymap = {};
citymap['chicago'] = {
  center: new google.maps.LatLng(41.878113, -87.629798),
  population: 2714856
};
citymap['newyork'] = {
  center: new google.maps.LatLng(40.714352, -74.005973),
  population: 8405837
};
citymap['losangeles'] = {
  center: new google.maps.LatLng(34.052234, -118.243684),
  population: 3857799
};
citymap['vancouver'] = {
  center: new google.maps.LatLng(49.25, -123.1),
  population: 603502
};

var cityCircle;

function radiusChanged() {
  document.getElementById('info').innerHTML = "<b>" + this.title + "</b><br>radius=" + this.radius.toFixed(2) + " km";
};

function initialize() {
  // Create the map.
  var mapOptions = {
    zoom: 4,
    center: new google.maps.LatLng(37.09024, -95.712891),
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

  // Construct the circle for each value in citymap.
  // Note: We scale the area of the circle based on the population.
  for (var city in citymap) {
    var populationOptions = {
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      editable: true,
      draggable: true,
      title: city,
      map: map,
      center: citymap[city].center,
      radius: Math.sqrt(citymap[city].population) * 100
    };
    // Add the circle for this city to the map.
    cityCircle = new google.maps.Circle(populationOptions);
    google.maps.event.addListener(cityCircle, 'radius_changed', radiusChanged);
  }
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 500px;
  width: 500px;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="info"></div>
<div id="map-canvas" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>

【讨论】:

    猜你喜欢
    • 2012-07-08
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    相关资源
    最近更新 更多