在春季动态添加谷歌地图(MVC或Boot)



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

存储商店的位置很容易,因为它只是一个包含纬度和经度的类。它可能是这样的(有关更多信息,请参阅 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 控制器,该控制器应在以下代码段中注入值(来自 Google 文档(:

<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>

最新更新