0

I have a basic SpringBoot app. using Spring Initializer, JPA, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file. I have a Thymeleaf that gets lat and lng variables from the controller. In the Thymeleaf template I have this piece of code

    <div id="Map" class="map"></div>
                        <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
                        <script th:inline="javascript">
                        /*<![CDATA[*/

                            var lat            = /*[[${lat}]]*/;
                         var lng            = /*[[${lng}]]*/;

                               var map = new ol.Map({
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            target: 'Map',
            controls: ol.control.defaults({
              attributionOptions: {
                collapsible: false
              }
            }),
            view: new ol.View({
              center: [lng, lat],
              zoom: 10
            })
          });

          document.getElementById('zoom-out').onclick = function() {
            var view = map.getView();
            var zoom = view.getZoom();
            view.setZoom(zoom - 1);
          };

          document.getElementById('zoom-in').onclick = function() {
            var view = map.getView();
            var zoom = view.getZoom();
            view.setZoom(zoom + 1);
          };

 /*]]>*/
                                  </script>
                </div>

but I see all the map in grey and when I see the source code of the page I see literally this:

  <script>
                    /*<![CDATA[*/

                        var lat            = 31.184349060058594;
                     var lng            = 1.2457042932510376;

                           var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        target: 'Map',
        controls: ol.control.defaults({
          attributionOptions: {
            collapsible: false
          }
        }),
        view: new ol.View({
          center: [lng, lat],
          zoom: 10
        })
      });

      document.getElementById('zoom-out').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom - 1);
      };

      document.getElementById('zoom-in').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom + 1);
      };
  /*]]>*/   
                    </script>

in the controller:

model.addAttribute("lat",           getLatitude());
        model.addAttribute("lng",           getLongitude());
2
  • 1
    On a side note, the coordinates should be long, lat, and not lat,long Commented Jun 11, 2018 at 12:45
  • 1
    Can you share your controller code? Commented Jun 11, 2018 at 13:58

1 Answer 1

2

There are couple of issues in your code.

  1. There is no id called "zoom-out" or "zoom-in". So document.getElementById('zoom-in').onclick = function() { gives an error.

  2. Please set the zoom level to 5. Then you can see the map. Actually map is loading but you can't see it because of it is already zoomed in.

This is the working html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Map</title>
</head>
<body>
<div id="Map" class="map"></div>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script th:inline="javascript">
    /*<![CDATA[*/

    var lat            = /*[[${lat}]]*/;
    var lng            = /*[[${lng}]]*/;

    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],
        target: 'Map',
        controls: ol.control.defaults({
            attributionOptions: {
                collapsible: false
            }
        }),
        view: new ol.View({
            center: ol.proj.fromLonLat([lon, lat]),
            zoom: 5
        })
    });

    document.getElementsByClassName('ol-zoom-out').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom - 1);
    };

    document.getElementsByClassName('ol-zoom-in').onclick = function() {
        var view = map.getView();
        var zoom = view.getZoom();
        view.setZoom(zoom + 1);
    };

    /*]]>*/
</script>
</div>
</body>
</html>

Please find the working example from here https://gitlab.com/supun/spring-boot-app/commit/856cbbd1e798ab071118d420f9cab02722d219eb

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks ... I've removed "zoom-out" and "zoom-in, set the zoom level to 5, but the only thing is taht is not centered to lat / long given ???
See my modified answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.