我正在使用 JPA、Servlet 和 JSP 进行我正在处理的 google map 项目.
JPA 实体称为“位置”
"Locations" 实体类有一个构造函数:
public Locations(Double latitude,Double longitude,String name,int people)
{this.latitude = latitude; this.longitude=longitude,this.name=name, this.people=people}
我有一个名为 LocationData 的类,由此我在名为 getLocation 的方法中返回数据库中所有位置的 ArrayList 及其 latitude,longitude,PeopleAvailableNow
:
ArrayList locations = new ArrayList() ;
List<Location> locations = query.getResultList(); //This contains all the Locations
for (Location s : locations) {
locations.add(new Location(s.lat,s.lon,s.name,s.people))
}
return locations
在我的 servlet 中,我传递了位置列表:
ArrayList list = ld.getLocations();
String json = new Gson().toJson(list);
response.setContentType("text/json");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(json);
在我的 JSP 使用的 map.js 中
// get locations from server
$.getJSON("locations", {}, function(data){
$.each(data, function(index, element){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(element.lat, element.lng),
map: map,
title: element.name,
people : element.people,
});
markers.push(marker);
现在,清楚地了解程序的运行方式,所有标记都出现在 map 上,我使用信息窗口显示标记(位置)的人员和名称。问题是,数据库中的人数总是会发生变化,因为一个人可以离开一个地方去另一个地方。
- 我怎样才能始终以特定的时间间隔刷新(从实体读取)“people”值?
- 我的设计是否无法适应这种情况,我该如何改变它才能满足这种能力?
最佳答案
我设法让它发挥作用。
我所做的是,我在 LocationData java 类中创建了一个方法,该方法具有人名的输入参数,并基于该方法返回该人的属性。
然后我创建了第二个 servlet,这个 servlet 只返回人的值(value),而不是像后者那样的所有人。我写在servlet的doGet
String personname = request.getParameter("personName"); //This will be received from an Ajax call
ArrayList list = ld.getLocation(personname);
String json = new Gson().toJson(list);
response.setContentType("text/json");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(json);
在 Javascript 中,我有一个叫做 ajax 的东西放在标记的按钮点击中,它可以获取数据
$.ajax({
dataType: "json",
type: 'GET',
url: 'servleturl',
data: {"personName": marker.getTitle()},
success: function(response) {
//do stuff here to perform the unique action to the marker}
});
关于java - 只刷新这部分数据(谷歌地图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23321450/