Como estilizar recursos criados pelo controle DrawFeature?


9

Eu tenho seguido este tutorial: http://workshop.pgrouting.org/chapters/geoext_client.html#select-the-start-and-final-destination

Ele contém um controle Openlayers.Control.DrawFeatures definido no seguinte exemplo de código. Você também pode ver as linhas nas quais o autor comenta "se queremos aplicar um estilo especial ao ponto de partida, devemos fazer isso aqui" . O problema é: não sei como aplicar um estilo nessa configuração e não consigo encontrar nenhum exemplo usando o controle DrawFeatures dessa maneira.

Como posso fazer com que o ponto inicial use um estilo diferente do ponto final usando esse controle DrawFeatures?

DrawPoints = OpenLayers.Class(OpenLayers.Control.DrawFeature, {

// this control is active by default
autoActivate: true,

initialize: function(layer, options) {
    // only points can be drawn
    var handler = OpenLayers.Handler.Point;
    OpenLayers.Control.DrawFeature.prototype.initialize.apply(
            this, [layer, handler, options]
        );
},

drawFeature: function(geometry) {
    OpenLayers.Control.DrawFeature.prototype.drawFeature.apply(
            this, arguments 
        );
    if (this.layer.features.length == 1) {
        // we just draw the startpoint
        // note: if we want to apply a special style to the 
        //       start point we should do this here
    } else if (this.layer.features.length == 2) {
        // we just draw the finalpoint
        // note: if we want to apply a special style to the 
        //       final point we should do this here

        // we have all what we need; we can deactivate ourself.
        this.deactivate();            
    }
}
});

Respostas:


7

adicione estas linhas e modifique-as para se adequar ao seu estilo:

...
    if (this.layer.features.length == 1) {
        // we just draw the startpoint
        // note: if we want to apply a special style to the 
        //       start point we should do this here

        var myFirstPointStyle = OpenLayers.Util.applyDefaults(myFirstPointStyle, OpenLayers.Feature.Vector.style['default']);
        myFirstPointStyle.fillOpacity = 0.8;
        myFirstPointStyle.strokeWidth = 2;
        myFirstPointStyle.fillColor = "#ffffff";
        this.layer.features[this.layer.features.length - 1].style = myFirstPointStyle;

        this.layer.redraw();

    } else if (this.layer.features.length == 2) {
        // we just draw the finalpoint
        // note: if we want to apply a special style to the 
        //       final point we should do this here
        var mySecondPointStyle = OpenLayers.Util.applyDefaults(mySecondPointStyle, OpenLayers.Feature.Vector.style['default']);
        mySecondPointStyle.fillOpacity = 0.8;
        mySecondPointStyle.strokeWidth = 7;
        mySecondPointStyle.pointRadius = 12;
        mySecondPointStyle.fillColor = "#000000";
        this.layer.features[this.layer.features.length - 1].style = mySecondPointStyle;

        this.layer.redraw();


        // we have all what we need; we can deactivate ourself.
        this.deactivate();
    }
...

Isso copiará o estilo padrão e você poderá modificá-lo a partir daí. Você deve obter algo como isto:

insira a descrição da imagem aqui


Ótimo, obrigado! Parece que chamar redraw () é a chave aqui. Eu nunca tentei isso :)
underdark

Você é bem-vindo, é sempre gratificante poder ajudar para fora um mestre :)
CaptDragon

Muito obrigado, pois resolveu o meu problema também que estava relacionado com a aplicação de estilos
GSTomar
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.