使用Spring MVC在Google Map上显示标记

时间:2015-05-19 21:15:16

标签: javascript google-maps spring-mvc google-maps-api-3

我试图通过获取纬度和经度数据库来显示标记。问题是我被困在视图部分。

我将与我的jsp文件共享,我想在其中使用JavaScript API将标记列表显示到Google Map中。

map.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <title>Auxilium Saver</title>
    <style>
        #map-canvas {
          height: 600px;
          height: 600px;
          margin: 0px;
          padding: 0px
        }
    </style>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?"></script>

    <script type="text/javascript">
        var markerLat, markerLong;
        markerLat = [
            <c:forEach var="s" items="${listGeo}" varStatus="status">
                [<c:out value="${s.latitude}"/>,
            </c:forEach>];
        markerLong = [
          <c:forEach var="s" items="${listGeo}" varStatus="status">
              [<c:out value="${s.longitude}"/>,
          </c:forEach>];

        function initialize() {
            var map;
            var initlatlng = new google.maps.LatLng(markerLat[0],markerLong[0]);
            var mapOptions = {
                zoom: 6,
                center: new google.maps.LatLng(36,5),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
            var infowindow = new google.maps.InfoWindow(); 
            var marker, i;

            for (i = 0; i < markerLat.length; i++) {
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(markerLat[i], markerLong[i]),
                    map: map
                });

                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        infowindow.setContent(markers[i]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        }

        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
</head>
</html>

MapController.java

@Controller
public class MapController {

    @Autowired
    private GeolocDAO geolocDAO;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView getPages(HttpServletRequest request) {
        Geolocation geoloc = geolocDAO.get();
        ModelAndView model = new ModelAndView("map");
        List<Geolocation> listGeo = geolocDAO.list();
        model.addObject("listGeo", listGeo);
        return model;
    }

}

2 个答案:

答案 0 :(得分:1)

如评论中所述,您生成的JS无效。了解如何在示例中生成markerLatmarkerLong

markerLat = [
        [51.5286417,

        [48.1549108,

        [34.0204989,

        [41.3947901,
    ];
markerLong = [
      [-0.1015987,

      [11.5418358,

      [-118.4117325,

      [2.1487679,
  ];

嗯,这不是有效的JavaScript,正如Google Chrome(或任何其他浏览器,当然还有规范)所说:

JavaScript Error

所以,在你的map.jsp中,更改此内容:

markerLat = [
    <c:forEach var="s" items="${listGeo}" varStatus="status">
        [<c:out value="${s.latitude}"/>,
    </c:forEach>];
markerLong = [
  <c:forEach var="s" items="${listGeo}" varStatus="status">
      [<c:out value="${s.longitude}"/>,
  </c:forEach>];

为此:

markerLat = [
    <c:forEach var="s" items="${listGeo}">
        <c:out value="${s.latitude}"/>,
    </c:forEach>
];
markerLong = [
  <c:forEach var="s" items="${listGeo}">
      <c:out value="${s.longitude}"/>,
  </c:forEach>
];

有了这个,将生成一个有效的JavaScript,如下所示:

markerLat = [
    51.5286417,

    48.1549108,

    34.0204989,

    41.3947901,
];
markerLong = [
    -0.1015987,

    11.5418358,

    -118.4117325,

    2.1487679,
];

这是我测试的结果:

Map result

答案 1 :(得分:0)

我遵循了您的示例,遇到了这些错误。

Errors in browser