简体   繁体   English

使用jQuery Store Locator插件以编程方式选择标记

[英]Programatically select a marker with jQuery Store Locator Plugin

I'm trying to programatically select a marker on a map with the jQuery Store Locator Plugin . 我正在尝试使用jQuery Store Locator插件以编程方式在地图上选择一个标记。

I have a jQuery click event that should trigger a marker selection. 我有一个jQuery click事件,应触发标记选择。

// Init
$('#bh-sl-map-container').storeLocator({ //settings })

$('.select-head-office').click(function() {
  // Here I'd like to select a specific marker, 
  // Ideally, select a marker from a unique category. Ex: Head office
});

The plugin has the method changeSelectedMarker(), not sure if I could use it to solve my problem. 该插件具有方法changeSelectedMarker(),不确定是否可以使用它来解决问题。

I finally figured a solution. 我终于找到了解决方案。 It's a little hackish but it works well. 有点破烂,但效果很好。

First, I realized I could use the list of locations to get the markers IDs, and associate them with the location category. 首先,我意识到我可以使用位置列表来获取标记ID,并将其与位置类别相关联。

<li data-markerid="{{markerid}}" data-marker-category="{{category}}">

Then, with jQuery, I select get the markerId for the location with the category 'head-office'. 然后,使用jQuery,选择类别为“总部”的位置的markerId。 (Ideally, a unique category) (理想情况下,是唯一类别)

$('*[data-marker-category='head-office']').data('markerid');

I created a custom method to pan to the selected marker. 我创建了一个自定义方法来平移到选定的标记。

selectMarker: function(markerId) {
 var _this = this;
 var map = _this.map;
 var infowindow = _this.infowindow;
 var storeStart = _this.storeStart;
 var page = _this.page;
 var selectedMarker = markers[markerId];

 map.panTo(selectedMarker.getPosition());
 if (_this.settings.bounceMarker === true) {
  selectedMarker.setAnimation(google.maps.Animation.BOUNCE);
  setTimeout(function () {
    selectedMarker.setAnimation(null);
  }, 1200);
 }
}

Finally, I call my method with the markerid variable I got earlier. 最后,我使用之前获得的markerid变量来调用我的方法。

storeLocator = $map_container.data('plugin_storeLocator');
storeLocator.selectMarker(activeMarkerID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM