2011-09-17 24 views
7

En una versión anterior de mi programa utilicé markers para marcar puntos en el mapa. En la versión actual tuve que cambiar de markers a vectors, porque necesito la flexibilidad adicional. En la solución marcadores utilicé la función de abajo para agregar una ventana emergente de la caja a un marcador:¿Cómo agregar un cuadro emergente a un vector en OpenLayers?

function createPopupBoxFeature(vector, lonLat, description) { 
    var feature = new OpenLayers.Feature(vector, lonLat); 

    feature.closeBox = true; 
    feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, 
     { "autoSize": true }); 
    feature.data.popupContentHTML = description; 

    vector.events.register("mousedown", feature, function(evt) { 
     if (this.popup == null) { 
      this.popup = this.createPopup(this.closeBox); 
      map.addPopup(this.popup); 
      this.popup.show(); 
     } else { 
      this.popup.toggle(); 
     } 
     OpenLayers.Event.stop(evt); 
    }); 

    return feature; 
} 

Pero ya no está funcionando para vectors, porque no tienen events propiedad. ¿Cómo puedo solucionar esto?

Respuesta

7

Resuelto por mí mismo. Aquí es cómo:

// Used to display the dialog popup 
var selectControl; 
var selectedFeature; 

Añadir un SelectFeature

selectControl = new OpenLayers.Control.SelectFeature(vectorLayer, 
    { 
     onSelect: onFeatureSelect, 
     onUnselect: onFeatureUnselect 
    }); 
    map.addControl(selectControl); 
    selectControl.activate(); 

Los controladores de eventos

function onPopupClose(evt) { 
    selectControl.unselect(selectedFeature); 
} 
function onPopupFeatureSelect(feature) { 
    selectedFeature = feature; 
    popup = new OpenLayers.Popup.FramedCloud("chicken", 
     feature.geometry.getBounds().getCenterLonLat(), 
     null, feature.name, null, true, onPopupClose); 
    popup.panMapIfOutOfView = true; 
    popup.autoSize = true; 
    feature.popup = popup; 
    map.addPopup(popup); 
} 
function onPopupFeatureUnselect(feature) { 
    map.removePopup(feature.popup); 
    feature.popup.destroy(); 
    feature.popup = null; 
} 

almacenar el contenido de la ventana emergente en el nombre del vector. Puede haber una solución mejor, pero no me importa. Agregar ventanas emergentes a vectores ya es bastante difícil.

vector.name = "Your popup content"; 
12

En realidad la forma oficial de hacerlo es la siguiente:

(Nota: algunas de las variables que no han sido declarados en estos fragmentos: longt, lat, mapa)

http://dev.openlayers.org/examples/light-basic.html

//Step 1 - create the vector layer 
var vectorLayer = new OpenLayers.Layer.Vector("ExampleLayer",{ 
    eventListeners:{ 
     'featureselected':function(evt){ 
      var feature = evt.feature; 
      var popup = new OpenLayers.Popup.FramedCloud("popup", 
       OpenLayers.LonLat.fromString(feature.geometry.toShortString()), 
       null, 
       feature.attributes.message+"<br>"+feature.attributes.location, 
       null, 
       true, 
       null 
      ); 
      popup.autoSize = true; 
      popup.maxSize = new OpenLayers.Size(400,800); 
      popup.fixedRelativePosition = true; 
      feature.popup = popup; 
      map.addPopup(popup); 
     }, 
     'featureunselected':function(evt){ 
      var feature = evt.feature; 
      map.removePopup(feature.popup); 
      feature.popup.destroy(); 
      feature.popup = null; 
     } 
    } 
}); 

//Step 2 - add feature to layer 
var p = new OpenLayers.Geometry.Point(longt, lat); 
var feature = new OpenLayers.Feature.Vector(
    p.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject()), 
    {message:'foo', location:'bar'}, 
    {externalGraphic: '../img/marker.png', graphicHeight: 21, graphicWidth: 16} 
); 
vectorLayer.addFeatures(feature); 

//Step 3 - create the selectFeature control 
var selector = new OpenLayers.Control.SelectFeature(vectorLayer,{ 
    hover:true, 
    autoActivate:true 
}); 

//Step 4 - add the layer and control to the map 
map.addControl(selector); 
map.addLayer(vectorLayer); 
Cuestiones relacionadas