Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!