【发布时间】:2015-12-15 11:58:35
【问题描述】:
我有一个带有表单的网页,一旦输入纬度和经度(连同其他数据)并提交,这些数据就会保存在 mongo 数据库中,同时在谷歌地图上放置一个标记。所以,当我提交新的纬度/经度数据时,我在地图上添加了一个新的标记等等。现在的问题是,当我切换到网站的另一个页面并返回上一页(带有地图)时,我仍然有地图,但没有标记。因此,每次我使用地图进入页面时,我都希望显示数据库中 lat/lng 的标记。换句话说,我想从数据库中检索数据(如果有的话)并在每次打开/切换到该页面时在地图上显示标记。 我在实现代码时遇到了一些问题,如果有人可以查看以下代码并告诉我我做错了什么,我将不胜感激。
提前感谢您的帮助。
代码如下:
AdminWebControler.java
/**
*
* @output ModelAndView
* @description redirect to dashboard
*/
@RequestMapping(value = "/adminDashboard", method = RequestMethod.GET)
public ModelAndView adminPage() {
userDao.updateLastLogin(getCurrentUser());
final ModelAndView model = new ModelAndView();
model.addObject("iLiteList", getAllILites());
model.setViewName("adminDashboard");
return model;
}
WebcontrollerUtilities.java`
protected List<ILites> getAllILites(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
List<ILites> ilites = mongoOperation.findAll(ILites.class, "ILites");
System.out.println("4. Number of Ilites in the database = " + ilites.size());
return ilites;
}
ILites.java
package mongodb.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection = "ILiteDB")
public class ILites {
private Double latitude;
private Double longitude;
/**
*
* @param longitude
* @param latitude
*/
public ILites( final Double latitude,final Double longitude) {
super();
this.longitude = longitude;
this.latitude = latitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
}
adminDashboard.jsp(部分jsp代码(地图显示所在行))
<div class="row">
<h4>
<strong>I-Lite Network View</strong>
</h4>
<div>
<hr class="Horizontal_divider_main" />
</div>
<c:choose>
<c:when test="${not empty iLiteList}">
<div class="row" id="googleMap" style="width: 100%; height: 50%; margin-left: 3px;"> </div>
</c:when>
<c:otherwise>
<script type="text/javascript">
var lat = new array();
<c:forEach items="${iLiteList}" var="ilites" >
//lat.push('${ilites.latitude}');
lat.push('<c:out value='${ilites.latitude}' />');
</c:forEach>
var lng = new array();
<c:forEach items="${iLiteList}" var="ilites" >
//lng.push('${ilites.longitude}');
lng.push('<c:out value='${ilites.longitude}' />');
</c:forEach>
for (i = 0; i < lat.length; i++) {
var latLng = new google.maps.LatLng(lat[i], lng[i]);
var marker = new google.maps.Marker({
position: latLng,
map: map,
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({
content: 'Latitude: ' + location.latitude + '<br>Longitude: ' + location.longitude
});
google.maps.event.addListener(marker, 'click', function() {
coordinates(map, marker, infowindow)});
function coordinates(map, marker,infowindow){
infowindow.open(map,marker);
}
}
</script>
</c:otherwise>
</c:choose>
</div>
map.js
var myCenter=new google.maps.LatLng(48.2188,11.6248);
var map;
function initialize()
{
var mapProp = {
center:myCenter,
zoom:18,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
}
google.maps.event.addDomListener(window, 'load', initialize
【问题讨论】:
标签: javascript java mongodb google-maps-api-3 jstl