Search code examples
htmlspring-mvcspring-bootthymeleafopenlayers

Thymeleaf with OpenLayers 4


I have this piece of code in a template :

 view: new ol.View({
                               center: ol.proj.fromLonLat([/*[[${center.longitude}]]*/, /*[[${center.latitude}]]*/]),
                           zoom: 14
                         })

this is the value of the object:

center [ Coordinate [latitude=41.33434906005859, longitude=1.8457042932510377]]

but when I see the source of the template I see this

view: new ol.View({
                               center: ol.proj.fromLonLat([[[${center.longitude}]], /*41.33434906005859*/]),
                           zoom: 14
                         })

Solution

  • Please assign the values into js variables first.

    var centerLat = /*[[${center.longitude}]]*/;
    var centerLng = /*[[${center.latitude}]]*/;
    

    Then use it.

     view: new ol.View({
            center: ol.proj.fromLonLat([centerLng, centerLat]),
            zoom: 5
        })
    

    Find the working code in here