简体   繁体   English

将Google地图标记绑定到非地图侧栏条目

[英]Binding Google Map Markers to non-map Sidebar Entries

Am trying to add 2-way bindings between event listeners on Google Map V3 markers and off-the-map sidebar entries. 我正在尝试在Google Map V3标记上的事件侦听器和地图外的侧边栏条目之间添加双向绑定。 I seek examples that accomplish this but am having trouble locating best practices. 我寻求实现此目标的示例,但是在寻找最佳实践时遇到了麻烦。 ESA 2009 was able to establish this type of association between sidebar entry and marker right after Google Maps API V3 was released. 在发布Google Maps API V3之后,ESA 2009能够在侧边栏条目和标记之间建立这种类型的关联。 Is there a contemporary method for accomplishing this, possibly with jQuery.Callbacks()? 有没有当代的方法可以做到这一点,可能使用jQuery.Callbacks()? This ESA 2009 Example has been up for 3 years but there have been few (if any) successful emulating implementions that have been able to cleanly duplicate the ESA 2009 elegant bidirectional binding functionality between the marker and the corresponding non-Map sidebar entry. 这个ESA 2009示例已经使用了3年,但是很少有(如果有的话)成功地模拟实现,能够清晰地复制标记和相应的非Map侧边栏条目之间的ESA 2009优雅的双向绑定功能的实现。 This PlugIn is a great start, but that JQuery lacks ability to highlight or identify the selected sidebar item when the marker is clicked directly. 此插件是一个很好的开始,但是当直接单击标记时,JQuery不能突出显示或标识所选的侧边栏项目。

The ESA 2009 approach uses internal functions, closures and prototypes in SidebarItem() and makeMarker() JS functions to establish two-way binding between sidebar entry and marker. ESA 2009方法使用SidebarItem()和makeMarker()JS函数中的内部函数,闭包和原型在边栏条目和标记之间建立双向绑定。 When the marker is clicked the sidebar entry is highlighted (and vice versa) without apparent memory leak or excessive recursion. 单击标记时,侧栏条目将突出显示(反之亦然),而不会出现明显的内存泄漏或过多的递归。 The ESA 2009 technique is conceptually difficult for me to grasp or extend. 从概念上讲,ESA 2009技术对我来说很难掌握或扩展。 The behavior is useful but not widely adopted. 该行为是有用的,但未被广泛采用。 When users click on a sidebar item, there is typical Google Maps behavior: the map pans and infowindow opens. 当用户单击侧边栏项目时,会出现典型的Google Maps行为:地图平移并打开信息窗口。 This is standard, but in the ESA 2009 version there is also an extra piece which is the non-map sidebar item listening and responding (through CSS with focus) to the corresponding marker click. 这是标准的,但是在ESA 2009版本中还有一个额外的功能,即非地图侧边栏项目侦听和响应(通过具有焦点的CSS)对相应的标记单击。 This 2-way listener effect has been difficult to port through JQuery or just to incorporate natively in any recent store locator solutions. 这种2路侦听器效果很难通过JQuery进行移植,也很难将其本机整合到任何最近的商店定位器解决方案中。 The desired functionality is: 所需的功能是:

When user clicks a marker or it's sidebar entry, existing highlights are cleared and the selected infowindow opens and corresponding sidebar item is identified in parallel...and vice versa. 当用户单击标记或其侧边栏条目时,将清除现有突出显示的内容,并打开选定的信息窗口,并并行标识相应的侧边栏项目...反之亦然。

What is the best practice for achieving this useful effect? 实现此有用效果的最佳实践是什么? Does anyone know of current examples integrating bi-directional triggers? 有人知道集成双向触发器的当前示例吗? How is the link best achieved between specific markers rendered from Google's servers and corresponding event listeners on the same page but outside of the map (eg, sidebar items). 如何在从Google服务器渲染的特定标记和同一页面但在地图外部(例如,侧边栏项目)的相应事件侦听器之间最佳地实现链接。 Should we be using any of JQuery's most recent (1.7) callback handling features? 我们是否应该使用JQuery的最新(1.7)回调处理功能中的任何一个? Are there better non-JQuery techniques? 是否有更好的非JQuery技术?

Below is the "state-of-the-art" familiar snippet creating the Marker and InfoWindow on a Map() named 'map' and creating a sidebar row in a DIV named 'sidebar' with markerArray and markerBounds collecting the markers. 下面是熟悉的“最新技术”代码段,在名为“ map”的Map()上创建Marker和InfoWindow,并在DIV中创建名为“ sidebar”的侧边栏行,其中markerArray和markerBounds收集了这些标记。 The SidebarItem() constructor is called by the makeMarker() function from ESA 2009's excellent example: SidebarItem()构造函数由ESA 2009优秀示例的makeMarker()函数调用:

 var infoWindow = new google.maps.InfoWindow();
 var markerBounds = new google.maps.LatLngBounds();
 var markerArray = [];

    function makeMarker(options){
    var pushPin = new google.maps.Marker({map:map});
    pushPin.setOptions(options);
    google.maps.event.addListener(pushPin, "click", function(){
      infoWindow.setOptions(options);
      infoWindow.open(map, pushPin);
      if(this.sidebarButton)this.sidebarButton.button.focus();
    });
    var idleIcon = pushPin.getIcon();
    if(options.sidebarItem){
      pushPin.sidebarButton = new SidebarItem(pushPin, options);
  pushPin.sidebarButton.addIn("sidebar");
}
markerBounds.extend(options.position);
markerArray.push(pushPin);
     return pushPin;
  }

google.maps.event.addListener(map, "click", function(){
  infoWindow.close();
});

function SidebarItem(marker, opts){
var tag = opts.sidebarItemType || "button";
var row = document.createElement(tag);
row.innerHTML = opts.sidebarItem;
row.className = opts.sidebarItemClassName || "sidebar_item";  
row.style.display = "block";
row.style.width = opts.sidebarItemWidth || "120px";
row.onclick = function(){
google.maps.event.trigger(marker, 'click');
}
row.onmouseover = function(){
google.maps.event.trigger(marker, 'mouseover');
}
row.onmouseout = function(){
google.maps.event.trigger(marker, 'mouseout');
}
this.button = row;
}
// adds a sidebar item to a 

SidebarItem.prototype.addIn = function(block){
if(block && block.nodeType == 1)this.div = block;
else
this.div = document.getElementById(block)
|| document.getElementById("sidebar")
|| document.getElementsByTagName("body")[0];
this.div.appendChild(this.button);
}
// deletes a sidebar item
SidebarItem.prototype.remove = function(){
if(!this.div) return false;
this.div.removeChild(this.button);
return true;
}

SidebarItem() and makeMarker() are public domain functions but they have been stubbornly difficult for newbies like me to understand, adopt and integrate. SidebarItem()和makeMarker()是公共领域的函数,但是对于像我这样的新手来说,它们一直很难理解,采用和集成。 Any advice or current examples would be appreciated. 任何建议或当前的例子,将不胜感激。 Specifically, is the ESA 2009 approach obsolete? 具体来说,ESA 2009方法是否已过时? I am not finding recent usage of this strategy. 我找不到该策略的最新用法。 What is the best modern way to associate non-map DOM elements with specific Google Map markers? 将非地图DOM元素与特定的Google地图标记关联的最佳现代方法是什么?

Thanks in advance... 提前致谢...

I don't think there's any magic in what you are trying to achieve but you do need to be aware of variable scope. 我认为您要实现的目标没有任何魔术,但您确实需要意识到可变范围。 The easiest approach is to declare your map markers and sidebar entries, and define their event handlers, in the same scope (eg. in an onload handler) so they can address each other. 最简单的方法是在同一范围内(例如,在onload处理程序中)声明地图标记和侧边栏条目,并定义其事件处理程序,以便它们可以相互寻址。 I haven't been through your code in detail so you may already be OK in this regard. 我没有详细阅读您的代码,因此您在这方面可能已经可以了。

You might also like to try working with .triggerHandler() rather than .trigger() otherwise there's a possibility that A will trigger B and B will immediately trigger A etc. etc. If so, then the call stack may fill up or the browser may just seize. 您可能还想尝试使用.triggerHandler()而不是.trigger()否则A可能会触发B,B会立即触发A等。如果是这样,则调用堆栈可能会填满,或者浏览器会填满可能只是抓住。


EDIT: 编辑:

Here is something similar, taken from an application in which Google map markers and corresponding external links are all given mouseover and mouseout handlers for mutual highliting. 这是从一个应用程序中获得的类似信息,在该应用程序中,为Google地图标记和相应的外部链接都提供了mouseover和mouseout处理程序,以相互增强显示效果。 The whole application is several thousands of lines long and organised into namespaces, each with a capitalised name (eg. GOOGLEMAP , DATA ). 整个应用程序有数千行,并组织成命名空间,每个命名空间都使用大写名称(例如GOOGLEMAPDATA )。

You will see that the data structure DATA.locationData is key to the interaction between the markers and external links. 您将看到数据结构DATA.locationData是标记和外部链接之间交互的关键。 It allows handlers to be attached in different namespaces (ie. different scopes) to links and markers. 它允许将处理程序附加到链接和标记的不同名称空间(即不同范围)中。

Namespace "GOOGLEMAP" (simplified) : 命名空间“ GOOGLEMAP”(简体):

var GOOGLEMAP = (function(){//NAMESPACE pattern
    //...
    var mouseoverLocMarker_closure = function(loc) {
        return function() {
            $(loc.link).addClass('hilight');
        };
    };
    var mouseoutLocMarker_closure = function(loc) {
        return function() {
            $(loc.link).removeClass('hilight');
        };
    };
    //...
    var init = function(...) {
        //...
        for(var name in DATA.locationData) {
            //...
            var loc = DATA.locationData[name];//lookup in a large hardcoded data structure in the DATA namespace
            loc.marker = new google.maps.Marker({ map:map, icon:'images/symbols/green.gif', zIndex:0 });//makes the marker available in other scopes
            //Here we attach functions returned by "_closure" functions, in order to correctly remember loc.
            google.maps.event.addListener( loc.marker, 'mouseover', mouseoverLocMarker_closure(loc) );
            google.maps.event.addListener( loc.marker, 'mouseout', mouseoutLocMarker_closure(loc) );
            //...
        }
    };
    //...
})();

A jQuery "document ready" closure (simplified) : jQuery“文档就绪”关闭(简化):

$(function(){
    $('#towns a.location').each(function(){
        //...
        var loc = DATA.locationData[name];//lookup in a large hardcoded data structure in the DATA namespace
        if(loc){
            //...
            $this.mouseover(function(){
                if(loc.marker) {
                    loc.icon = loc.marker.getIcon();
                    loc.marker.setIcon('images/symbols/red.gif');
                }
            });
            $this.mouseout(function(){
                if(loc.marker) {
                    loc.marker.setIcon(loc.icon);
                }
            });
            loc.link = this;//makes the link available to other scopes
        }
        else {
            //...
        }
    });
    //...
});

I can't honestly say that this code is exemplary - it could probably be more efficient of memory - but it is well organised and thoroughly reliable. 我不能老实地说这段代码是示例性的-可能可以提高内存的效率-但它组织得很好并且完全可靠。

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

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