javascript - 如何在一个页面上显示多个自定义的谷歌地图?

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

我需要在单个页面上的不同 map 上显示多个项目的坐标。

为了实现这一点,我需要执行类似于下面代码的操作。问题是如何将 map 名称和坐标列表传递给 JavaScript?

我可以传递的值是以下三个值

    ${location.lat}
    ${location.long}
    ${location.mapName}

JSP

我循环遍历列表以显示每个位置的名称,我还尝试将每个 map 的 ID 设置为彼此不同。

<c:forEach var="location" items="${locations}">
    <div id="item">
          <h1>${location.name}</h1>
          <div id="${location.mapName}"></div>      
    </div>
</c:forEach>

<script>
...
var myOptions = {
    zoom: 14,
    center: new google.maps.LatLng(0.0, 0.0),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
    map = new google.maps.Map(document.getElementById("map_canvas"),
                                myOptions);

    map2 = new google.maps.Map(document.getElementById("map_canvas2"),
                                   myOptions);  
}

</script>

enter image description here

最佳答案

首先,从Location开始是一个java对象,你不能有一个名为long的属性。另外,<div id="item">相反forEach将在 DOM 上生成具有重复 id 的对象。

为了测试,我创建了一个 Location像这样的对象:

public class Location {

    private double lat;
    private double lng;
    private String mapName;

    private String name;

    // getters and setters

}

简单来说,只需迭代locations即可将位置写入 map ,如下所示:

<c:forEach var="location" items="${locations}">
    <h5>${location.name}</h5>
    <div id="${location.mapName}" style="height: 180px; width: 400px;"></div>
</c:forEach>

并迭代生成 map 对象、标记等,如下所示:

<c:forEach var="location" items="${locations}">
    var latLng_${location.mapName} = new gm.LatLng(${location.lat}, ${location.lng});
    var options_${location.mapName} = {
        zoom: 14,
        center: latLng_${location.mapName},
        mapTypeId: gm.MapTypeId.TERRAIN
    };

    var ${location.mapName} = new gm.Map(document.getElementById("${location.mapName}"), options_${location.mapName});

    var marker_${location.mapName} = new gm.Marker({
        title: "${location.name}",
        position: latLng_${location.mapName},
        map: ${location.mapName}
    });
</c:forEach>

如果需要,您可以将其包含在函数中。在测试中我生成了 4 张 map ,如下图所示:

Test with multiple maps

这是整个 JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"></script>

<script type="text/javascript">
    var gm = google.maps;
    function initialize() {
    <c:forEach var="location" items="${locations}">
        var latLng_${location.mapName} = new gm.LatLng(${location.lat}, ${location.lng});
        var options_${location.mapName} = {
            zoom: 14,
            center: latLng_${location.mapName},
            mapTypeId: gm.MapTypeId.TERRAIN
        };

        var ${location.mapName} = new gm.Map(document.getElementById("${location.mapName}"), options_${location.mapName});

        var marker_${location.mapName} = new gm.Marker({
            title: "${location.name}",
            position: latLng_${location.mapName},
            map: ${location.mapName}
        });
    </c:forEach>
    }

    gm.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<c:forEach var="location" items="${locations}">
    <h5>${location.name}</h5>
    <div id="${location.mapName}" style="height: 180px; width: 400px;"></div>
</c:forEach>
</body>
</html>

关于javascript - 如何在一个页面上显示多个自定义的谷歌地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29980862/

相关文章:

javascript - 如何在 PhoneGap 的 windows 平台上实现 map (Google 或 Bing)?

java - 如何在 JSP 声明中使用 JSTL

java - 从 index.html 重定向时,重定向应该是另一个 HTML 还是 JSP 页面?

javascript - 在 Windows 7 上结合 Jasmine 和 Visual Studio 2010 运行 JSCover

javascript - 有条件地加载 javascript 文件中的 js 部分 WordPress

javascript - 是否可以在绘制 Google Maps API 折线时取消它?

R 尝试查找欧洲城市的纬度/经度数据并获取地理编码错误消息

eclipse - 在 Eclipse 中更改 web.xml <servlet-mapping> 无效(Eclipse Juno、Tomcat 6)

javascript - 数组对象之间的映射关系

javascript - 如何使用 laravel 重命名已存储在服务器中的文件?