0

我编写了以下代码来显示标记,并有各自的信息窗口显示警察局的名称。警察局类有名称、纬度和经度。

    <script>
        function initialize()
        {
            var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
            var myCenter = new google.maps.LatLng(28.6523605,77.0910645);
            var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

           var mapProp = {
                center: myCenter,
                zoom: 10,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                scaleControl: true
            };


            var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
            var marker = [];
            var info= [];

            <%
            ArrayList<PoliceStation> stationList = new PoliceStation().loadclass();
            for (int i=0 ; i < stationList.size() ; i++) {
            %>

                var pos = new google.maps.LatLng(
                            <%= stationList.get(i).getLatitude()%>,
                            <%= stationList.get(i).getLongitude()%>
                            );

                marker = new google.maps.Marker({
                    position: pos,
                    map: map
                });

                marker.setMap(map);
                var MarkerContent = "<div class=\"marker\">" +
                                    "<h2>"+ "<%=stationList.get(i).getstationName()%>" 
                                    +"</h2>"+ 
                                    "</div>";


                info=new google.maps.InfoWindow();
                info.setOptions({
                content: MarkerContent,
                size: new google.maps.Size(50, 50),
                position: pos
            });

            google.maps.event.addListener(marker, 'click', function () {
                info.setOptions({
                content: MarkerContent
            });
                info.open(map, marker);
            });


        <%
        }
        %>

所有标记都按预期设置。但是,单击任何标记会导致仅打开最后绘制的标记的信息窗口。

我知道这是因为所有标记,infoWindow 名称都是多余的,最后只有最后一个 infoWindow 被保存到最后一个标记。

我遇到了将内容封装在 for 循环中的解决方案

(function() {var marker = ...; google.maps.event.addListener...;})(); 

但是,我正在寻找一种动态明确定义每个变量名称的解决方案。例如,将每次迭代时递增的计数器的值附加到每个标记和信息变量。请不要提供 JSTP 解决方案。寻找特定的 JSP 解决方案

4

1 回答 1

0

令人惊讶的是它真的很容易...... JSP 基本上是用来动态打印 HTML 的。所以,而不是使用标记变量作为

var marker = new google.maps.Marker({
                    position: pos,
                    map: map
                });

利用 -

var <%out.print("marker"+i);%> = new google.maps.Marker({
                    position: pos,
                    map: map
                });

需要注意的是——

var <%out.print("marker["+i+"]");%> = new google.maps.Marker({
                    position: pos,
                    map: map
                });

对我不起作用。

上面的正确代码创建了新变量,其值为marker1、marker2 ...等

还有事件侦听器内的内容更新功能-

info.setOptions({
            content: MarkerContent
        });

不需要。

于 2013-12-10T07:09:49.623 回答