Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
OpenLayers: how to add dynamic markers onClick with Longitude and Latitude?

Currently working on a map for an interface where users click on a map to get their longitude and latitude. So far I have managed to get the lon and lat data and have it input in the form, but having difficulties displaying a marker where the user has clicked, and removing it when the user clicks on a new location. any ideas?

this is my function:

initLocationPicker: function (){

    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],

        target: 'map',
        view: new ol.View({
            center: [13.4050,52.5200],
            zoom: 2
        })
    });

    var formId = this.formId;
    var form = document.forms[formId];


    map.on('singleclick', function (evt) {

        var lonlat = ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');
        var lon = lonlat[0];
        var lat = lonlat[1];
        console.log(lonlat);

        form.elements.longitude.value = lon
        form.elements.latitude.value = lat

    });
},

I hope this snippet helps, otherwise i'll try and add a jsfiddle.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda