2011-11-27 60 views
2

有沒有人有如何使用具有開放圖層點的事件處理程序的示例?打開圖層點事件

感謝

function mapCreate(lon,lat){ 
     map = new OpenLayers.Map("map1"); 
     var osm = new OpenLayers.Layer.OSM(); 
     vectors = new OpenLayers.Layer.Vector("Vector Layer"); 
     map.addLayer(osm); 
     var center = new OpenLayers.LonLat(lon,lat).transform(
      new OpenLayers.Projection("EPSG:4326"), 
      map.getProjectionObject() 
     ); 

     point = new OpenLayers.Geometry.Point(center.lon,center.lat); 
     vectors.addFeatures([new OpenLayers.Feature.Vector(point)]); 
     drag = new OpenLayers.Control.DragFeature(vectors); 
     //map.addLayer(vectors); 
     map.addControl(drag); 
     drag.activate(); 
     map.setCenter(center, 15); 
     map.addLayer(vectors); 
     point.events.register('moveend',point, function(evt){ 
      alert('hello'); 
     }); 

    } 

這是我所嘗試過的例子,由於某種原因,這部分不工作

point.events.register('moveend',point, function(evt){ 
       alert('hello'); 
      }); 
+0

感謝代碼更新,我沒有區分標記和點,你怎麼樣?標記是人們用來識別給定點的東西,不是? – jcolebrand

+0

我區分這兩個因爲點屬於矢量和標記不。我認爲。從查看標記的api,可以找到可以解決我的問題的事件屬性。 – Paul

+0

啊,非常好,我明白你的意思了。另外,希望你現在可以解決它。 – jcolebrand

回答

0

下面是我在過去使用一些類似的代碼在頁面右側的div列表上懸停顯示標記。包括它,因爲它展示了我過去的觀點。我不認爲這是你想要的。

/* Included for per-item hovering from the paginated layer. */ 
function onFeatureSelected(event) { 
    hoveredItem = $(this).attr('lookup'); 

    /* Do something here to indicate the onhover */ 
    // find the layer pagination id 
    var feature = findFeatureById(hoveredItem); 

    if (feature) { 

     // use the pagination id to find the event, and then trigger the click for that event to show the popup 
     // also, pass a null event, since we don't necessarily have one. 
     feature.marker.events.listeners.click[0].func.call(feature, event) 
    } 
} 
function onFeatureUnselected(event) { 
    /* Do something here to indicate the onhover */ 
    // find the layer pagination id 
    var feature = findFeatureById(hoveredItem); 

    if (feature) { 

     // use the pagination id to find the event, and then trigger the click for that event to show the popup 
     // also, pass a null event, since we don't necessarily have one. 
     feature.marker.events.listeners.click[0].func.call(feature, event) 
    } 

    /* Do something here to stop the indication of the onhover */ 

    hoveredItem = null; 
} 

function findFeatureById(featureId) { 
    for (var key in map.layers) { 
     var layer = map.layers[key]; 
     if (layer.hasOwnProperty('features')) { 
      for (var key1 in layer.features) { 
       var feature = layer.features[key1]; 
       if (feature.hasOwnProperty('id') && feature.id == featureId) { 
        return feature; 
       } 
      } 
     } 
    } 
    return null; 
} 

如果你想爲正在創建它這個加的地步,我必須刷新我就怎麼說在過去已經做內存。或者,GIS SE應該對您有所幫助。