Google maps api 3 扩展多段线并处理鼠标事件
我编写了JS应用程序,其中我使用点数组绘制了许多多段线,但在avery point中,我在该点中有一些附加属性(GPS数据、速度等) 我想在mouseover或onmouseclick事件上显示这些附加道具 我有两种方法:Google maps api 3 扩展多段线并处理鼠标事件,google-maps-api-3,event-handling,mouseevent,polyline,extending,Google Maps Api 3,Event Handling,Mouseevent,Polyline,Extending,我编写了JS应用程序,其中我使用点数组绘制了许多多段线,但在avery point中,我在该点中有一些附加属性(GPS数据、速度等) 我想在mouseover或onmouseclick事件上显示这些附加道具 我有两种方法: 使用标准多段线和事件处理程序。但在这种情况下,我无法确定此多段线起点的其他属性,因为我无法将这些道具保存在多段线属性中。有一种解决方案-在阵列中保存其他属性,并尝试通过对多段线的第一个点进行板条来查找它们,但我想这太慢了 扩展多段线并在新对象中保存其他属性,但无法扩展鼠标事件
function myPolyline(prop, opts){
this.prop = prop;
this.Polyline = new google.maps.Polyline(opts);
}
myPolyline.prototype.setMap = function(map) {
return this.Polyline.setMap(map);
}
myPolyline.prototype.getPath = function() {
return this.Polyline.getPath();
}
myPolyline.prototype.addListener= function(prop) {
return this.Polyline.addListener();
}
myPolyline.prototype.getProp= function() {
return this.prop;
}
myPolyline.prototype.setProp= function(prop) {
return this.prop = prop;
}
并在for循环中创建新对象(i-点数组中当前点的索引),如下所示:
var polyline_opts = {
path: line_points,
strokeColor: color,
geodesic: true,
strokeOpacity: 0.5,
strokeWeight: 4,
icons: [
{
icon: lineSymbol,
offset: '25px',
repeat: '50px'
}
],
map: map
};
var add_prop = {
id: i,
device_id: device_id
};
...
devices_properties[device_id].tracks[(i-1)] = new myPolyline(add_prop, polyline_opts);
var tmp = devices_properties[device_id].tracks[(i-1)];
google.maps.event.addListener(tmp.Polyline, 'click', function(e) {
...
console.log(tmp.prop.id);
...
}
其中:
- 线_点-点阵列(仅两个点)
- i-当前点索引
- devices_properties[device_id]。跟踪-按我的device_id索引添加属性的扩展多段线数组
var polyline_opts = {
path: line_points,
strokeColor: color,
geodesic: true,
strokeOpacity: 0.5,
strokeWeight: 4,
icons: [
{
icon: lineSymbol,
offset: '25px',
repeat: '50px'
}
],
map: map
};
var add_prop = {
id: i,
device_id: device_id
};
...
devices_properties[device_id].tracks[(i-1)] = new myPolyline(add_prop, polyline_opts);
var tmp = devices_properties[device_id].tracks[(i-1)];
google.maps.event.addListener(tmp.Polyline, 'click', function(e) {
...
console.log(tmp.prop.id);
...
}
但在这种情况下,我总是在控制台中获得相同的id
当我使用
google.maps.event.addListener(devices_properties[device_id].tracks[(i-1)].Polyline, 'click', function(e) {
...
console.log(???); // How to get parent of polyline fired the event?
...
}
我不知道如何让多段线的父对象触发事件?我回答了我自己的问题-完成了,我只是在使用“for”而不是“$”时遇到了一些问题。每个“:) 使用前:
for ( i = 1; i < devices_properties[device_id].history_points.length; i++ ) {
...
create myPolyline
...
}
它可以工作-创建许多事件处理程序
要处理事件,我使用以下方法:
google.maps.event.addListener(c_polyline.Polyline, 'mouseover', function(e) {
var prop = c_polyline.getProp();
...
console.log(prop.id, prop.device_id);
}
我回答我自己的问题-已经完成了,我只是在使用“for”而不是“$.each”时遇到了一些问题: 使用前:
for ( i = 1; i < devices_properties[device_id].history_points.length; i++ ) {
...
create myPolyline
...
}
它可以工作-创建许多事件处理程序
要处理事件,我使用以下方法:
google.maps.event.addListener(c_polyline.Polyline, 'mouseover', function(e) {
var prop = c_polyline.getProp();
...
console.log(prop.id, prop.device_id);
}
我回答我自己的问题-完成了,我只是在使用“for”而不是“$.each”时遇到了一些问题:)我回答我自己的问题-完成了,我只是在使用“for”而不是“$.each”时遇到了一些问题:)你知道for循环实现不起作用的原因吗?如果可能的话,我会尽量避免使用jQuery,因为在我的应用程序中其他地方不需要它。你有没有想过为什么for循环实现不起作用?如果可能的话,我会尽量避免使用jQuery,因为我的应用程序中其他地方不需要它。