Tell me more ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I've been trying to create a feature programatically in OpenLayers, but the feature moves to 0,0 whenever I zoom in or out.

This is just an ajax request, that filters a bunch of points using their names. If I get just one feature back, I draw it on the map, reading the geometry value from a field (geometriaTexto). After that I transform it to 900913 projection and construct a full feature.

The feature is added just fine. But whenever I zoom in out or out, it goes to the map origin (0,0).

Check some code:

    var buscarPontoInteresse = function(termo, posicao, e) {
        $.ajax({
            type: "GET",
            url: "/geo/poi/pesquisa",
            dataType: "json",
            data: { t: termo },
            success: function(retorno) {

                if (retorno.length > 1) {
                    $("#boxResultados").html("<div class='modal-header'><h3>Resultados</h3></div><div class='modal-body'>Resultados</div>");
                }
                else if (retorno.length <= 0) {
                    $("#boxResultados").html("<p>Não foram encontrados resultados para o termo.</p>");
                }
                else {

                    var formato = new OpenLayers.Format.WKT();

                    var f = retorno[0];
                    var app = e.data.app;
                    var camada = app.camadaNovosPontos;
                    var geometriaTexto = f.fields.geometria;
                    var geometriaLida = OpenLayers.Geometry.fromWKT(geometriaTexto);
                    geometriaLida = geometriaLida.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
                    var feicao = new OpenLayers.Feature.Vector(geometriaLida,{
                        nome: f.fields.nome,
                        indice: posicao
                    });
                    feicao.geometry.calculateBounds();

                    if (camada.features[posicao])
                        camada.features[posicao].destroy();

                    camada.addFeatures([feicao]);
                    // camada.drawFeature(feicao);

                    var caixaTexto = "";
                    if (posicao == 0) {
                        caixaTexto = "#id_de";
                    }
                    else {
                        caixaTexto = "#id_ao";
                    }

                    $(caixaTexto).val(f.fields.nome);
                }
            }
        });
    }

Anyone has a great tip or solution for this?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.