【问题标题】:Adding Google Maps dynamically in Spring (MVC or Boot)在 Spring(MVC 或 Boot)中动态添加谷歌地图
【发布时间】:2020-02-14 07:33:15
【问题描述】:

我正在 Spring Boot 中开发租赁系统上的应用程序。我需要帮助来动态添加谷歌地图标记并为我添加的每个房间显示它。
例如:在添加房间详细信息的同时,还将每个房间的谷歌地图标记保存到 MySQL 数据库中并显示它。

【问题讨论】:

  • 说实话你在这里找一个完整的项目,最好在github上搜索
  • 没有搜索完整的项目.. 项目的一部分.. 而且它在 github 中无处可用

标签: java spring spring-boot spring-mvc


【解决方案1】:

存储商店的位置很容易,因为它只是一个包含纬度和经度的类。可能是这样的(更多信息,see the guide by Spring):

@Entity
public class Position {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Long id;
  private Long latitude;
  private Long longitude;

  @OneToOne
  private Store store;

  protected Position() {}

  public Position(long latitude, longitude, Store store) {
    this.latitude = latitude;
    this.longitude = longitude;
    this.store = store;
  }

  // Standard Getters and Setters
}

使用此对象,您可以将纬度和经度传递给 MVC 控制器,该控制器应在以下代码中注入值(from the Google Documentation)

<div id="map"></div>
<script th:inline="javascript">
  /*<![CDATA[*/
  function initMap() {
    // The location of the map
    var shop = { lat: /*[[${session.shop.latitude}]]*/, lng: /*[[${session.shop.longitude}]]*/ };
    var map = new google.maps.Map(
      document.getElementById('map'), { zoom: 4, center: shop });
    var marker = new google.maps.Marker({ position: shop, map: map });
  }
/*]]>*/
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
</script>

【讨论】:

  • 谢谢您,先生.. 我希望您不会介意我回到您身边,以防万一有任何困惑。再次感谢
  • 一个问题,我该如何显示呢?在 JSP 页面中.. 显示添加的谷歌地图标记?
  • @SaubhagyaSharma 如果我回答了您的问题,请接受我的回答和/或投票。我的答案包括一种使用百里香叶在页面上显示它的方法。将其重构为 JSP 应该不难。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多