Show route with several Google Map bookmarks

1

I'm trying to do a google map that marks a route with several points, I saw an example on the google maps page. To do this I need an array with the intermediate positions. I have the positions in a mysql bd but I do not know how to pass the addresses to javascript.

This is RutaCompleta.jsp (I do not know if you can do that from c: foreach with <script> )

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Ruta Completa</title>
        <link href="repartidores/css/mapa.css" rel="stylesheet" type="text/css"/>
        <script src="repartidores/js/mapaRutaCompleta.js" type="text/javascript"/>
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBnGP5-NeoiUKVYrv32QL7ydnM3ji4NIro&callback=initMap"
        async defer></script>
        <c:forEach items="${repartos}" var="reparto">
            <script type="text/javascript">
                waypoints.push({
                    location: '${reparto.pedido.direccion}',
                    stopover: true
                });
            </script>
        </c:forEach>
    </head>
    <body>
        <div id="map">

        </div>
    </body>
</html>

mapaRutaCompleta.js

var posActualLng = 0;
var posActualLat = 0;
var waypoints = [];

function obtenerUbicacion(){
    if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position) {
            posActualLat = position.coords.latitude;
            posActualLng = position.coords.longitude;

        }, function() {
            alert("Error");
        });
    } 
    else {
        // El navegador no soporta Geolocation
        alert("El navegador no soporta geolocation");
    }
}



// MOSTRAR MAPA
function initMap() {
    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 6,
        center: {lat: posActualLat, lng: posActualLng}
    });

    directionsDisplay.setMap(map);
    calculateAndDisplayRoute(directionsService, directionsDisplay);
}

function calculateAndDisplayRoute(directionsService, directionsDisplay) {

  directionsService.route({
    origin: {lat: -34.378522, lng: -55.234380},
    destination: {lat: -34.880044, lng: -56.078461},
    waypoints: waypoints,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else {
      alert('Directions request failed due to ' + status);
    }
  });
}

RutaCompletaServlet.java

@WebServlet(name = "RutaCompletaServlet", urlPatterns = {"/RutaCompletaServlet"})
public class RutaCompletaServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Consulta cons, cons1, cons2;
    try {
        cons = new Consulta("r.id, idRepartidor, idPedido, tiempo", "repartos r, pedidos p");
        cons1 = new Consulta("*", "pedidos");
        cons2 = new Consulta("*", "usuarios");
        ResultSet rs = cons.recuperar("idRepartidor = 11 and r.idPedido = p.id and p.estado <> 'ENTREGADO'");
        List<Repartos> repartos = new ArrayList<>();
        // recorro el rs con los repartos
        while (rs.next()){
            int idPed = rs.getInt("idPedido");
            ResultSet rs1 = cons1.recuperarPorId(idPed);
            // recorro el rs1 con los pedidos
            while(rs1.next()){
                Pedido pedido = new Pedido(rs1.getInt("id"), rs1.getString("descripcion"), rs1.getInt("precio"), rs1.getString("direccion"), rs1.getString("fechaHora"), rs1.getString("comentario"), rs1.getString("estado"));

                ResultSet rs2 = cons2.recuperarPorId(rs.getInt("idRepartidor"));
                // recorro el rs2 con los usuarios
                while (rs2.next()){
                    Usuario repartidor = new Usuario(rs2.getInt("documento"), rs2.getString("nombre"), rs2.getString("apellido"), rs2.getString("domicilio"), rs2.getString("telefono"), rs2.getString("nomUsuario"), rs2.getString("password"), rs2.getString("rol"));
                    Repartos rep = new Repartos(rs.getInt("id"), repartidor, pedido);
                    repartos.add(rep);
                }                  
            }
        }

        request.setAttribute("repartos", repartos);
        request.getRequestDispatcher("/repartidores/RutaCompleta.jsp").forward(request, response);

    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(ConsultaPedidosServlet.class.getName()).log(Level.SEVERE, null, ex.getMessage());
    }

}
    
asked by Juan Manuel 16.11.2016 в 00:35
source

1 answer

1

I think the problem you have is that you want the server to mount the html and run the javascript to build the map data. The truth is that I do not know if it would work but I see it as a very elaborate solution.

I think it would be easier for you to solve it from javascript directly. You paint your view with the map and from javascript (already in the client when the page has been loaded) you make a call to your servlet that returns the points. You go through it and paint what you need.

    
answered by 25.11.2016 / 09:05
source