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

236
Visualizações
map.getView().fit() and image problems in openlayers

I want to make an "Add Area" function. After clicking on the map the popup for entering the Area name will be showing and after confirming the name, the map should be automatically zooming to the area with padding {100,100,100,100} and the image marker will be showing. But my image marker(.png and .svg) is not showing, then it zooms too deep. It seems my padding didn't work well.

AddArea.js

import 'ol/ol.css';
import Draw from 'ol/interaction/Draw';
import Map from 'ol/Map';
import View from 'ol/View';
import {toLonLat} from 'ol/proj';
import {toStringXY} from 'ol/coordinate';
import {OSM, Vector as VectorSource} from 'ol/source';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import {fromLonLat} from 'ol/proj';
import Swal from 'sweetalert2/dist/sweetalert2.js'
import 'sweetalert2/src/sweetalert2.scss'

const map=$('#map').data('map');

const vectorSource = new VectorSource();
const vector = new VectorLayer({
  source: vectorSource,
  style: new Style({
     image: new Icon({
         anchor: [0.5, 1],
         anchorXUnits: "fraction",
         anchorYUnits: "fraction",
         src: "pin.png"
     })
 })
});

var vectorLayer = new VectorLayer({
  source: new VectorSource(),
});
map.addLayer(vectorLayer);

let draw;
function addInteraction() {
    map.addLayer(vector);
    draw = new Draw({
    source: vectorSource,
    type: "Point",
  });
map.addInteraction(draw);
}


$('#add-area-btn').click(function(){
   addInteraction();

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

Swal.fire({
  position: 'top',
  title: 'Input area name',
  input: 'text',
  inputPlaceholder: 'Enter area name',
  showCancelButton: true,
  allowOutsideClick: false,
  inputValidator: (value) => {
     
    if (!value) {
      return 'You need to write something!'
    }
    else {

      map.getView().fit(vectorSource.getExtent(),  
      {
        size: map.getSize,
        padding:[100,100,100,100]
      });
      vector.getSource().clear(true);
      map.removeLayer(vector);
      map.getInteractions().pop();

      //Enable click function
      map.un('click', callback);
    }
  }
}).then((result) => {
  //Cancel button clicked
  
  if (result.dismiss == Swal.DismissReason.cancel) {
    Swal.fire({
      position: 'top',
      icon: 'info',
      title: 'Area Canceled'
    });
    vector.getSource().clear(true);
    map.removeLayer(vector);
    map.getInteractions().pop();

    //Enable click function
    map.un('click', callback);
   }
  })
 });
})

That's my code and hope someone can help me... Thank you...

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