admin管理员组文章数量:1427478
I have an OpenLayers map with a marker and a popup that's supposed to appear when I click on the marker. This works fine in IE8 but not in Firefox 3.6. Any ideas why? As far as I can tell the mousedown event is not getting fired since my log message doesn't appear. The map is at .html and the code I use to create the marker and popup is:
function addMarker() {
var map = g_waze_map.map;
var markers1 = new OpenLayers.Layer.Markers( "Markers1" );
g_waze_map.map.addLayer(markers1);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('.png',size,offset);
var marker = new OpenLayers.Marker(new OpenLayers.LonLat(34.7934759272249, 32.0835554760902),icon);
markers1.addMarker(marker);
marker.events.register('mousedown', marker, function(evt) {
console.log('hi');
var popup = new OpenLayers.Popup.FramedCloud(null,
marker.lonlat,
null,
"<div style='background-color:red; width:150;height:100'>hi</div>",
null,true,null);
map.addPopup(popup);
OpenLayers.Event.stop(evt);
});
}
I have an OpenLayers map with a marker and a popup that's supposed to appear when I click on the marker. This works fine in IE8 but not in Firefox 3.6. Any ideas why? As far as I can tell the mousedown event is not getting fired since my log message doesn't appear. The map is at http://ndinfo.heroku./test.html and the code I use to create the marker and popup is:
function addMarker() {
var map = g_waze_map.map;
var markers1 = new OpenLayers.Layer.Markers( "Markers1" );
g_waze_map.map.addLayer(markers1);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://www.openlayers/dev/img/marker.png',size,offset);
var marker = new OpenLayers.Marker(new OpenLayers.LonLat(34.7934759272249, 32.0835554760902),icon);
markers1.addMarker(marker);
marker.events.register('mousedown', marker, function(evt) {
console.log('hi');
var popup = new OpenLayers.Popup.FramedCloud(null,
marker.lonlat,
null,
"<div style='background-color:red; width:150;height:100'>hi</div>",
null,true,null);
map.addPopup(popup);
OpenLayers.Event.stop(evt);
});
}
Share
Improve this question
asked Mar 9, 2011 at 11:05
JohnnyJohnny
7,38110 gold badges51 silver badges80 bronze badges
2 Answers
Reset to default 3Answer from here. The key was overriding the activate()
function in OpenLayers.Control.ModifyFeature
. I didn't realize having a control before creating the marker would affect the markers in any way, but it turns out that it does.
var shapes = new OpenLayers.Layer.Vector( "Shapes" );
map.addLayer(shapes);
var modifyControl = new OpenLayers.Control.ModifyFeature(shapes, {
activate: function() {
var activated = false;
if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) {
this.map.events.on({
"removelayer": this.handleMapEvents,
"changelayer": this.handleMapEvents,
scope: this
});
activated = true;
}
return activated;
}
});
I think that marker
doesn't have any 'mousedown' event associated. But OpenLayers.Markers
probably has it. Try this:
// Create your markers layer
var markerLayer = new OpenLayers.Layer.Markers( "Markers1" );
// do whatever you want, and then...
// Create your marker
var marker = new OpenLayers.Marker(
new OpenLayers.LonLat(34.7934759272249, 32.0835554760902),
icon);
// Add your recently created marker to your markers layer
markerLayer.addMarker(marker);
// And bind 'mousedown' event to 'markers' layer, not to 'marker' object
markerLayer.events.register('mousedown', markerLayer, function(evt) {
console.log('hi');
var popup = new OpenLayers.Popup.FramedCloud(null,marker.lonlat,null,
"<div style='background-color:red; width:150;height:100'>hi</div>",
null,true,null);
map.addPopup(popup);
OpenLayers.Event.stop(evt);
});
Here is a little example: http://jsbin./ezeno3 (click on the map to create the mark, and then click on the mark to open a popup).
I hope it helps. Happy codding!
本文标签: javascriptOpenLayers popup not responding to eventStack Overflow
版权声明:本文标题:javascript - OpenLayers popup not responding to event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745502852a2661110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论