从服务器端NodeJS渲染Google Map

时间:2018-11-25 16:00:29

标签: node.js google-maps express handlebars.js

如何将waypoints对象数组从服务器端传输到客户端javascript,以便可以在Google Maps中渲染折线?我试图避免GET请求,因为waypoints数组包含大量数据,并且url会变得太长。 我正在使用把手来呈现我的html内容。有没有使用把手response.render('index.hbs', {waypoints: waypoints});方法的方法?

index.hbs

<!DOCTYPE html>
 <html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>{{pageTitle}}</title>
  <link rel="stylesheet"href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

  <style>
  /* Always set the map height explicitly to define the size of the div
   * element that contains the map. */
  #map {
    height: 100%;
  }
  /* Optional: Makes the sample page fill the window. */
  html, body {
    height: 100%;
    margin: 0;
    padding: 0;
  }
</style>
 </head>
 <body>
  {{> clientnav }}

  {{#if hasData}}
    <h3>{{from}} to {{to}}</h3>


    <div id="map"></div>
     <script>

      // This example creates a 2-pixel-wide red polyline showing the path of
      // the first trans-Pacific flight between Oakland, CA, and Brisbane,
      // Australia which was made by Charles Kingsford Smith.

      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
         zoom: 3,
          center: {lat: 0, lng: -180},
          mapTypeId: 'terrain'
        });

        //Somehow get the waypoints array from my server side to here:
        const coordinatesFromMyServer = [];
        var flightPath = new google.maps.Polyline({
         path: flightPlanCoordinates,
         geodesic: true,
         strokeColor: '#FF0000',
         strokeOpacity: 1.0,
         strokeWeight: 2
        });

        flightPath.setMap(map);
      }
     </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCRqdENdwD5BXEnfWLsd8l5DdyZvveXevg&callback=initMap">
</script>


  {{else}}
   <h1>Nothing to see here...</h1>

  {{/if}}

  {{> bootstrap }}
</body>
</html>

0 个答案:

没有答案