OpenLayers弹出窗口没有响应事件

时间:2011-03-09 11:05:54

标签: javascript openlayers

我有一个带有标记的OpenLayers地图和一个当我点击标记时应该出现的弹出窗口。这在IE8中工作正常,但在Firefox 3.6中没有。有什么想法吗?据我所知,mousedown事件没有被解雇,因为我的日志消息没有出现。地图位于http://ndinfo.heroku.com/test.html,我用来创建标记和弹出窗口的代码是:

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.org/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);

                });
}

2 个答案:

答案 0 :(得分:3)

来自here的回答。关键是覆盖activate()中的OpenLayers.Control.ModifyFeature函数。我没有意识到在创建标记之前有一个控件会以任何方式影响标记,但事实证明它确实如此。

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;
        }
    });

答案 1 :(得分:1)

我认为marker没有关联任何'mousedown'事件。但是OpenLayers.Markers可能有它。试试这个:

// 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);
});

这是一个小例子:http://jsbin.com/ezeno3(点击地图创建标记,然后点击标记打开弹出窗口)。

我希望它有所帮助。快乐的编码!