GoogleMap API with Java/Springboot and Thymeleaf receiving the coordenates but not creating the map

huangapple go评论111阅读模式
英文:

GoogleMap API with Java/Springboot and Thymeleaf receiving the coordenates but not creating the map

问题

HTML 代码(我将只翻译代码部分,如下所示):

  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  6. <meta name="viewport" content="width=device-width" />
  7. <title>Mapa do cliente</title>
  8. </head>
  9. <body>
  10. <div class="mapa"
  11. th:attr="data-latitude=${customer.latitude},data-longitude=${customer.longitude}"
  12. id="map" style="width: 100%; height: 100%"></div>
  13. <script src="https://jquery.gocache.net/jquery-3.5.1.min.js"
  14. type="text/javascript"></script>
  15. <script src="/webjars/bootstrap/4.5.0/js/bootstrap.min.js"></script>
  16. <script type="text/javascript"
  17. src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&amp;libraries=&amp;v=weekly"></script>
  18. <script src="/js/mapFunctions.js"
  19. type="text/javascript"></script>
  20. </body>
  21. </html>

JavaScript 代码:

  1. let map;
  2. var lat = $('.mapa').attr('data-latitude');
  3. var lng = $('.mapa').attr('data-longitude');
  4. function initMap(lat, lng) {
  5. map = new google.maps.Map(document.getElementById("map"), {
  6. center: { lat, lng },
  7. zoom: 8
  8. });
  9. }
  10. initMap(parseFloat(lat), parseFloat(lng));

请注意,翻译可能会因格式限制而略有不同。如果您需要进一步的帮助,请随时提问。

英文:

I'm creating a web aplication in JAVA/SPRING/HTML5 with Thymeleaf that makes a map with markers based on a H2 Database with latitude and longitude of all customers. I was successful in the backend x front integration. The Java controller send a object to HTML page with Thymeleaf, that page import a javascript file with a google map generate function, consuming the object and executing the function. But the problem is: the map do not generate on the page

I debug the code and dont figure out whats the problem.

HTML CODE (I reduce the code to focus on the problem):

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
  3. xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
  4. &lt;head&gt;
  5. &lt;meta charset=&quot;UTF-8&quot; /&gt;
  6. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;
  7. &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width&quot; /&gt;
  8. &lt;title&gt;Mapa do cliente&lt;/title&gt;
  9. &lt;/head&gt;
  10. &lt;body&gt;
  11. &lt;div class=&quot;mapa&quot;
  12. th:attr=&quot;data-latitude=${customer.latitude},data-longitude=${customer.longitude}&quot;
  13. id=&quot;map&quot; style=&quot;width: 100%; height: 100%&quot;&gt;&lt;/div&gt;
  14. &lt;script src=&quot;https://jquery.gocache.net/jquery-3.5.1.min.js&quot;
  15. type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  16. &lt;script src=&quot;/webjars/bootstrap/4.5.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
  17. &lt;script type=&quot;text/javascript&quot;
  18. src=&quot;https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&amp;libraries=&amp;v=weekly&quot;&gt;&lt;/script&gt;
  19. &lt;script src=&quot;/js/mapFunctions.js&quot;
  20. type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  21. &lt;/body&gt;
  22. &lt;/html&gt;

JAVASCRIPT CODE:

  1. let map;
  2. var lat = $(&#39;.mapa&#39;).attr(&#39;data-latitude&#39;)
  3. var lng = $(&#39;.mapa&#39;).attr(&#39;data-longitude&#39;)
  4. function initMap(lat, lng) {
  5. map = new google.maps.Map(document.getElementById(&quot;map&quot;), {
  6. center: { lat, lng },
  7. zoom: 8
  8. });
  9. }
  10. initMap(parseFloat(lat), parseFloat(lng))

I print the 'lat' and 'lng' variables in console and they are ok. Im not a expert in javascript and english, so excuse-me whatever not good pratice.

答案1

得分: 1

你尝试过 $( document ).ready() 吗?当你调用 initMap(parseFloat(lat), parseFloat(lng)) 时,DOM 可能尚未渲染出你的地图元素。

英文:

Have you tried $( document ).ready() ? The DOM may not render your map element when you call initMap(parseFloat(lat), parseFloat(lng))

答案2

得分: 0

似乎是一个样式问题。地图没有渲染出来,我尝试将&lt;div id=&quot;map&quot;&gt;中的高度和宽度从100%更改为500像素,然后地图就显示出来了。所以,上面的代码确实实现了它的目的,如果进行了更改。当然有更好的做法,但为了教学方式,我认为这是一个有效的贡献。感谢大家的帮助。

英文:

Appears to be a style problem. How was not renderizing the map, i tryed to change the height and witdh of 100% to 500px in &lt;div id=&quot;map&quot;&gt;, and the maps showed up. So, the code above really attends its purpose, if the change are make. Sure there is better prattices, but for didatic way i think its a valid contribution. Thanks for the help guys.

huangapple
  • 本文由 发表于 2020年7月24日 11:22:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63066288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定