我正在构建一个网页,它将显示从Android应用程序发送的用户lat / langs。我已经设法接收了这些lat / lngs,并且还使用DAO和servlet类将它们插入到DB中。现在对jsp中的java脚本知之甚少。我如何将这些参数传递给这个jsp,以便它可以在地图上显示它们?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript">
</script>
<style>
<% int i= 0;%>
<c:forEach var="usinf" items="${LatLang}">
#map-canvas_<%=i%> {
height: 150px;
width: 250px;
margin: 0px;
padding: 0px
}
<% i= i+1;%>
</c:forEach>
</style>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script>
<script>
function initialize() {
<% i= 0;%>
<c:forEach var="usinf" items="${LatLang}">
var geocoder<%=i%>;
var mapsss<%=i%>;
var infowindow<%=i%> = new google.maps.InfoWindow();
var marker<%=i%>;
geocoder<%=i%> = new google.maps.Geocoder();
var latlng<%=i%> = new google.maps.LatLng(${usinf.pickuplat},${usinf.pickuplng});
geocoder<%=i%>.geocode({'latLng': latlng<%=i%>}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
mapsss<%=i%>.setZoom(11);
marker<%=i%> = new google.maps.Marker({
position: latlng<%=i%>,
map: mapsss<%=i%>
});
infowindow<%=i%>.setContent(results[1].formatted_address);
infowindow<%=i%>.open(mapsss<%=i%>, marker_<%=i%>);
}else {
alert('No results found');
}
}else {
alert('Geocoder failed due to: ' + status);
}
});
var mapOptions<%=i%> = {
zoom: 8,
center: latlng<%=i%>,
mapTypeId: 'roadmap'
}
mapsss<%=i%> = new google.maps.Map(document.getElementById('map-canvas_<%=i%>'), mapOptions<%=i%>);
<% i= i+1;%>
</c:forEach>
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<title>Insert title here</title>
</head>
<body>
<body onload="initialize()">
<div id="map-canvas_" style="width:100%; height:100%"></div>
</body>
</body>
</html>
我的servlet做post方法。
protected void Process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Authenticate authplots = new Authenticate();
List<UserInfo> getTheLatlangs = authplots.GetAllLangs();
request.setAttribute("LatLang", getTheLatlangs);
RequestDispatcher view = request.getRequestDispatcher("Testmaps.html");
view.forward(request, response);
}
答案 0 :(得分:0)
使用JSTL,我这样做了。也许它会有所帮助。
<script type="text/javascript">
var markers = [
<c:forEach var="row" items="${result.rows}">
['<c:out value="${row.aciklama}" />',
<c:out value="${row.KONUME}" />,
<c:out value="${row.KONUMB}" />,
<c:out value="${row.EVID}" />,
'<c:out value="${row.aptadi}" />',
'<c:out value="${row.mahalle}" />',
'<c:out value="${row.sokak}" />',
'<c:out value="${row.durumu}" />',
'<c:out value="${row.metrekare}" />',
'<c:out value="${row.fiyat}" />',
'<c:out value="${row.odasayisi}" />'],
</c:forEach> ];
</script>
你可以查看github。