简体   繁体   English

Google地图:可点击的折线图标

[英]Google Maps: clickable polyline icon

I have two markers with a polyline connecting the two. 我有两个带有折线的两个标记。 I have click events on the markers and the polyline, but i was trying to make the polyline easier to click without placing a new marker or increasing it's strokeWeight. 我在标记和折线上有单击事件,但是我试图使折线更易于单击,而无需放置新标记或增加它的strokeWeight。 So I created a circular icon and placed on the polyline, but I can't make it clickable. 因此,我创建了一个圆形图标并将其放置在折线上,但是我无法使其变为可点击状态。 Is it possible? 可能吗?

Saw this thread but doesn't give any specifics on how the icon is clickable. 看到了该线程,但未提供有关如何单击图标的任何详细信息。 I searched it's code source but he adds a KML layer. 我搜索了它的代码源,但他添加了一个KML层。 I didn't want to do that. 我不想那样做。 Google Maps: Attaching events on polyline icons Google地图:在折线图标上附加事件

Searched the google maps overlay API but didn't find any interface to listen to click events. 搜索了Google Maps overlay API,但未找到任何界面来监听点击事件。 https://developers.google.com/maps/documentation/javascript/overlays#Polylines https://developers.google.com/maps/documentation/javascript/overlays#Polylines

I also tried to attach an event listener but didn't work. 我也尝试附加一个事件监听器,但是没有用。 I suspect it can't be done without adding an actual marker or an object but if someone else had a similar problem i would appreciate any tips :) 我怀疑如果不添加实际的标记或对象就无法完成,但是如果其他人遇到类似的问题,我将不胜感激:)

Thanks in advance! 提前致谢!

My code: 我的代码:

var pathSymbol = {
    path: google.maps.SymbolPath.CIRCLE,
    scale: 8,
    strokeColor: '#228B22'
};

var conPath = new google.maps.Polyline({
    path: conCoord,
        strokeColor: "#228B22",
        strokeOpacity: 0.7,
        icons: [{
            icon: pathSymbol,
            offset: '50%'
        }],
        strokeWeight: 2
});

conPath.setMap(map);


google.maps.event.addListener(conPath, 'click', (function(l,conCoord) {
    return function() {
            infowindowPath.setContent("<b>Ligação "+connections[l].id);
        infowindowPath.setPosition(new google.maps.LatLngBounds(conCoord[1], conCoord[0]).getCenter());
            infowindowPath.open(map);
        }
})(l,conCoord));

I have a need for this functionality as well, but unfortunately it is not possible - I'm almost positive (see my demo ). 我也需要此功能,但不幸的是这是不可能的-我几乎是积极的(请参阅我的演示 )。 The reason I say so is because: 我这样说的原因是:

  1. I've tried lots of different ways, but only the Polyline receives the event 我尝试了许多不同的方法,但只有折线接收了事件
  2. It is not explicitly documented in Google's documentation 没有在Google文档中明确记录
  3. of what the following parts of the documentation implies: 文档的以下部分意味着什么:

    From the documentation on Symbols : 关于符号文档中

    A Symbol is a vector based image that can be displayed on either a Marker or a Polyline object. 符号是基于矢量的图像,可以显示在标记折线对象上。

    From the documenation on the AddEventListener : AddEventListener文档中

     addListener(instance:Object, eventName:string, handler:Function) addListener(instance:Object,eventName:string,handler:Function) 

    Adds the given listener function to the given event name for the given object instance. 将给定的侦听器函数添加到给定对象实例的给定事件名称。 Returns an identifier for this listener that can be used with removeListener(). 返回可以与removeListener()一起使用的此侦听器的标识符。

Events can be attached to Object instances (such as a Marker or Polyline ). 事件可以附加到Object 实例 (例如MarkerPolyline )。 Since Symbols are vector-based images that are rendered on a Polyline, they are contained within it, and not officially Object instances. 由于符号是在折线上渲染的基于矢量的图像,因此它们包含在其中,而不是正式的Object实例。 Apparently, this makes them ineligible to have events attached to themselves. 显然,这使他们不符合附加事件的资格。

Now, what I'm still left in doubt is that my rational above implies that a Symbol is part of the Polyline meaning it should also receive the same events that were attached to the Polyline. 现在,我仍然有疑问的是,我的上述合理论点意味着符号是折线的一部分,这意味着它也应该接收与折线相关的相同事件。 However, in my trials, this isn't the case ( demo here : regardless the size of the Symbol on a Polyline, it does not receive any events): 但是,在我的试验中,情况并非如此( 此处的演示 :无论折线上符号的大小如何,它都不会接收任何事件):

var mySymbol = {
    path: google.maps.SymbolPath.CIRCLE,
    scale: 25,
    strokeWeight: 5,
    fillOpacity: .2
};

var myPolyline = new google.maps.Polyline({
    icons: [{
        icon: mySymbol,
        fixedRotation: true,
        offset: '50%',
    }],
    path: [polylineCenter, polylineEnd],
    strokeColor: 'black',
    strokeOpacity: 1,
    strokeWeight: 5,
    map: myMap
});

// works since <myPolyline> is an instance of an Object
google.maps.event.addListener(myPolyline, 'click', function() {
    alert('Polyline clicked!');
});

// doesn't work :-( since <mySymbol> is an Object literal
google.maps.event.addListener(mySymbol, 'click', function() {
    alert('Symbol clicked!');
});

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

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