javascript - c :foreach tag is not working for Google map in javascript jsp

标签 javascript google-maps jsp google-maps-api-3 foreach

我必须使用 fatbase 中的纬度和经度值显示 GoogleMap,但它无法使用 c:foreach 标记工作,但我不知道 c:foreach 标记有什么问题。jsp 页面和托管bean 的代码如下:

   <%@page contentType="text/html" pageEncoding="UTF-8"%>
        <!DOCTYPE html>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
        <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
        <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
        <html> 
            <head> 
                <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
                <title>Google Maps Multiple Markers</title> 
                <script src="http://maps.google.com/maps/api/js?sensor=false"
                type="text/javascript"></script>
                <script type="text/javascript" src="js/jquery-2.1.1.js"></script>

            </head> 
            <body>
                <!--<div id="load_me">Current Time :<%= new java.util.Date() %></div>-->
                <input type="button" onclick="mapRefresh();" value="Refresh" class="button" />
                <div><br/></div>
                <div id="map" style="width: 1300px; height: 600px;"></div>

        <!--        <script type="text/javascript">
                    //alert("my alert");
                    var auto_refresh = setInterval(
                            function()
                            {
                                // alert("alert");
                                $('#load_me').load('mapview.jsp').fadeIn("slow");
                            }, 5000); // autorefresh the content of the div after
                    //every 10000 milliseconds(10sec)
                </script>-->
                <script type="text/javascript">

                    //alert("hello");
                    var locations = [
                    <c:forEach items="${mapBean.googleMapLocations}" var="googleLocation"  varStatus ="status">
                        ['${googleLocation.latitude}', '${googleLocation.longitude}']
                        <c:if test="${!status.last}">
                        ,
                        </c:if>
                    </c:forEach>
                    ];
                   alert(locations);
         var map = new google.maps.Map(document.getElementById('map'), {
                        zoom: 10,
                        center: new google.maps.LatLng("${mapBean.centerLatitude}", "${mapBean.centerLongitude}"),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    });
                    var infowindow = new google.maps.InfoWindow();
                    var marker, i;
                    for (i = 0; i < locations.length; i++) {
                        marker = new google.maps.Marker({
                            position: new google.maps.LatLng(locations[i][0], locations[i][1]),
                            map: map
                        });
                        google.maps.event.addListener(marker, 'click', (function(marker, i) {
                            return function() {
                                infowindow.setContent(locations[i][0]);
                                infowindow.open(map, marker);
                            }
                        })(marker, i));
                    }
         </script>
        <table>
                    <c:forEach var="googleLocation" items="${mapBean.googleMapLocations}">
                        <tr><td>${googleLocation.latitude}</td><td>${googleLocation.longitude}</td></tr>  
                    </c:forEach>
                    CENTER<tr><td>${mapBean.centerLatitude}</td><td>${mapBean.centerLongitude}</td></tr>
                </table>

            </body>
        </html>

Managedbean Code:
public class MapBean implements java.io.Serializable {

 private double centerLatitude;
    private double centerLongitude;
 public java.util.List<Components> getGoogleMapLocations() {
        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session sess = sf.openSession();
        Query q = sess.createQuery("from Components");
        java.util.List l = q.list();
        sess.close();
        return l;
    }
}

最佳答案

我想这就是您需要包含在 c:foreach 标记中的内容。

<c:out value="${googleLocation.latitude}', '${googleLocation.longitude}"></c:out> 

关于javascript - c :foreach tag is not working for Google map in javascript jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27265596/

相关文章:

javascript - 我怎样才能使谷歌地图气泡的高度更长?

java - jsp tomcat 与 netbeans 中的 c3p0 连接池

java - 如何在请求后刷新表单?

javascript - Jquery 下拉菜单无法正常工作

android - Google Play 服务更新 Android 应用程序

Javascript正则表达式匹配最后一个单词,无论特殊字符如何

javascript - 如何清除 map 上当前的标记

jsp - 使用 Spring MVC 3 显示文件 png、jpg、... JSP

javascript - mouseenter/mouseleave 动画链停止

javascript - 使循环等待ajax调用