gmaps4rails动态重置标记图标/图片的标准方法是什么?

时间:2012-07-18 16:36:03

标签: gmaps4rails

我想做的就是动态重置标记的图标。例如,假设红色标记固定到地图上的特定位置。发生用户交互,需要红色标记变黄。在这种情况下,我希望能够做到这样的事情:

      var icon = marker.serviceObject.getIcon();
      if ($("#chooseJobsForWorkSite ul li input:checked").size() > 0) {
        icon.url = "/assets/purple_MarkerT.png";
      } else {
        icon.url = "/assets/green_MarkerS.png";
      }
      marker.serviceObject.setIcon(icon);

这适用于单个标记,但是当我循环遍历此代码时 一个标记,出错了 - 我没有得到所有标记的所需颜色。

1 个答案:

答案 0 :(得分:1)

我使用replaceMarkers API方法解决了这个问题。这是有效的代码:

var replacementMarkers = new Array();
for (var k = 0; k < activeMarkers.length; ++k) {
  var marker = activeMarkers[k];

  var iconUrl = null;
  if ($.inArray(marker, taskMarkers) >= 0) {
    iconUrl = "/assets/purple_MarkerT.png";
  } else {
    iconUrl = "/assets/green_MarkerS.png";
  }

  replacementMarkers.push({
    "lat": marker.lat.toString(),
    "lng": marker.lng.toString(),
    "picture": iconUrl,
    "id": marker.id,
    "description": marker.description
  });
}

// Delay the replacement operation because in some instances, we reach this branch via the Gmaps.map.callback.
// In that case, I prefer to defer touching the gmap4rails until the current branch is done.

window.setTimeout(function() {
  Gmaps.map.replaceMarkers(replacementMarkers);
  resetAddressMarkers();
}, 25);