首先關閉所有,因爲Location
是一個Java對象,你不能有一個屬性,叫做long
。此外,<div id="item">
insede forEach
將在DOM上生成具有重複ID的對象。
對於測試,我創建了一個Location
對象是這樣的:
public class Location {
private double lat;
private double lng;
private String mapName;
private String name;
// getters and setters
}
在一個簡單的方法,只需遍歷locations
到CREAD位置的地圖,像這樣:
<c:forEach var="location" items="${locations}">
<h5>${location.name}</h5>
<div id="${location.mapName}" style="height: 180px; width: 400px;"></div>
</c:forEach>
然後迭代生成地圖對象,標記等等,如下所示:
<c:forEach var="location" items="${locations}">
var latLng_${location.mapName} = new gm.LatLng(${location.lat}, ${location.lng});
var options_${location.mapName} = {
zoom: 14,
center: latLng_${location.mapName},
mapTypeId: gm.MapTypeId.TERRAIN
};
var ${location.mapName} = new gm.Map(document.getElementById("${location.mapName}"), options_${location.mapName});
var marker_${location.mapName} = new gm.Marker({
title: "${location.name}",
position: latLng_${location.mapName},
map: ${location.mapName}
});
</c:forEach>
如果你願意,你可以把它放在一個函數中。在測試中,我產生4個地圖,這樣的形象:
這是整個JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
var gm = google.maps;
function initialize() {
<c:forEach var="location" items="${locations}">
var latLng_${location.mapName} = new gm.LatLng(${location.lat}, ${location.lng});
var options_${location.mapName} = {
zoom: 14,
center: latLng_${location.mapName},
mapTypeId: gm.MapTypeId.TERRAIN
};
var ${location.mapName} = new gm.Map(document.getElementById("${location.mapName}"), options_${location.mapName});
var marker_${location.mapName} = new gm.Marker({
title: "${location.name}",
position: latLng_${location.mapName},
map: ${location.mapName}
});
</c:forEach>
}
gm.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<c:forEach var="location" items="${locations}">
<h5>${location.name}</h5>
<div id="${location.mapName}" style="height: 180px; width: 400px;"></div>
</c:forEach>
</body>
</html>
謝謝隊友我會嘗試一下,請你也看看這個問題,http:// stackoverflow。com/questions/29982289/how-to-show-items-on-google-map-similar-to-the-way-google-shows-its-results –
@DanielNewtown好的,我儘快看到。 –
我在編碼上顯示錯誤消息「令牌上的語法錯誤,錯位 構造」「 –