1
Guys, I’m trying to create a page on my site that shows me title, description and the location of the record of some occurrences. When I access the page, I load through a <c:forEach>
all occurrences with their proper parameters. Occurrences are sent inside a list via Servlet to a JSP, which shows them.
My problem is that to load the map with the location of the occurrence, I need to execute a tag <script>
who has no body. The tag loads the files and, at the end of the upload, calls a javascript function, present on the page, using a callback="function()"
.
Because I am using this tag to load and call the function that shows my map with the occurrence location, I need it to be executed in a forEach
, for
or anything else that helps me loop. I thought about doing it with scriptlet or JSTL, but I couldn’t right away. Can anyone help me? I’m using the Google Maps javascript API.
JSP loading the location:
<%@page pageEncoding="ISO-8859-1"%>
<%@include file="includes/cabecalho.html"%>
<%@include file="includes/navmenu.jsp"%>
<div class="centralizado jumbotron">
<div class="card-deck">
<!--Panel-->
<div class="row">
<c:forEach var="ocorrencia" items="${ocorrencias}">
<div class="col-md-4">
<div style="padding: 10px;" class="card">
<div style="padding: 10px;" class="card-body">
<h5 class="card-title">${ocorrencia.tituloOcorrencia}</h5>
<p class="card-text">${ocorrencia.descricaoOcorrencia}</p>
<div style="position: relative; width: 100%;height: 300px" id="map"></div>
<input type="number" id="lat" value="${ocorrencia.latitudeOcorrencia}" hidden/>
<input type="number" id="lng" value="${ocorrencia.longitudeOcorrencia}" hidden/>
<br>
</div>
<div class="card-footer">
<small class="text-muted">${ocorrencia.usuario_fk_ocorrencia.nomeUsuario} em ${ocorrencia.dataOcorrencia}</small>
</div>
</div>
</div>
</c:forEach>
</div>
</div>
</div>
<script>
// Initialize and add the map
function initMap() {
//Attribuition for lat and lng to var
var lat = parseFloat(document.getElementById('lat').value);
var lng = parseFloat(document.getElementById('lng').value);
// The location of Uluru
var uluru = {lat: parseFloat(lat), lng: parseFloat(lng)};
// The map, centered at Uluru
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 15, center: uluru});
// The marker, positioned at Uluru
var marker = new google.maps.Marker({position: uluru, map: map});
}
</script>
<!--Load the API from the specified URL
* The async attribute allows the browser to render the page while the API loads
* The key parameter will contain your own API key (which is not needed for this tutorial)
* The callback parameter executes the initMap() function
-->
<!--Preciso do loop nesta tag-->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAUWfu8L-5YIKUDXV812EM3tBZhrK0uDY4&callback=initMap"></script>
<%@include file="includes/footer.jsp"%>
Some information that might be helpful to me: https://developers.google.com/maps/documentation/javascript/tutorial
Thank you so much for the help. The index of the.index loop is something like a "reserved variable", right?
– Welber Duarte
@Welberduarte I edited the answer to explain this better.
– Victor Stafusa
Beauty. Thanks @Victorstafusa.
– Welber Duarte