Ad
How To Use Foreach In Jstl + Javascript?
I try add pin in Google map. I have collection ${list}
items with coordinate and name. Everyone item I want put in map. I write some code:
<script>
function initMap() {
<c:forEach items="${list}" var="item">
var cairo = {lat: ${item.geometry.lat}, lng: ${item.geometry.lng}};
var map = new google.maps.Map(document.getElementById('map'), {
scaleControl: true,
center: cairo,
zoom: 10
});
var infowindow = new google.maps.InfoWindow;
infowindow.setContent('${item.name}');
</c:forEach>
}
But my code don't work correct. This add in map only one (last) item from collection.
Please help.
Ad
Answer
you may need create a Marker point to map
function initMap() {
var cairo =new google.maps.LatLng(38.54,77.02);
var map = new google.maps.Map(document.getElementById('map'), {
scaleControl: true,
center: cairo,
zoom: 10
});
<c:forEach items="${list}" var="item">
var infowindow = new google.maps.InfoWindow;
infowindow.setContent('${item.name}');
var myLatlng = new google.maps.LatLng(${item.geometry.lat}, ${item.geometry.lng});
var marker = new google.maps.Marker({
position : myLatlng,
map : map,
icon : 'img/icon.png'
title:${item.name}
});
google.maps.event.addListener(marker, 'click', function() {
// map.setZoom(15);
// map.setCenter(marker.getPosition());
infowindow.open(map, marker);
});
</c:forEach>
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad