templates/index/map.html.twig line 1

Open in your IDE?
  1. {% extends 'index/layout.html.twig' %}
  2. {% block title %}地図から探す{% endblock %}
  3. {% block stylesheet %}
  4.   <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css"/>
  5.   <link rel="stylesheet" href="/css/map.css">
  6. {% endblock %}
  7. {% block header %}
  8.   <header>
  9.     <div class="back">
  10.       <a href="{{ path('index') }}"><img src="img/detail/arrow.png" alt="戻る"></a>
  11.     </div>
  12.     <a href="#navi" class="navi-trigger drawer-toggle">
  13.         <span class="navi-icon">
  14.             <svg x="0px" y="0px" width="54px" height="54px" viewBox="0 0 54 54"></svg>
  15.         </span>
  16.     </a>
  17.     <button id="geolocation" style="" class="btn-location">
  18.       <svg height="20px" version="1.1" viewBox="0 0 16 16" width="20px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><title/><defs/><g fill="none" fill-rule="evenodd" id="Icons with numbers" stroke="none" stroke-width="1"><g fill="#000000" id="Group" transform="translate(-144.000000, -192.000000)"><path d="M144.017456,200 L160.019653,192.043091 L152,208.088623 L152,200 L144.017456,200 Z M144.017456,200" id="Path 221"/></g></g></svg>
  19.     </button>
  20.   </header>
  21. {% endblock %}
  22. {% block main %}
  23.   <main>
  24.     <div id="map-container"></div>
  25.     <section>
  26.       <div id="map-footer-container" class="footer-panel" style="max-height:300px;">
  27. {#        {% for shop in shops %}#}
  28. {#        <div class="shop_wr" id="shop-{{ shop.id }}" data-shop="{{ shop.id }}" data-lat="{{ shop.coordinate.latitude }}" data-lng="{{ shop.coordinate.longitude }}">#}
  29. {#          <div class="food_wr">#}
  30. {#            <ul class="clearfix slick-carousel">#}
  31. {#              {% for menu in shop.menus %}#}
  32. {#                <li>#}
  33. {#                  <a href="{{ path('shop', {id: shop.id} )}}" class="shadow"> </a>#}
  34. {#                  <div class="food_img" style="border-radius: 5px;" data-img="{{ menu.image|default('/img/no-image.jpeg') }}"></div>#}
  35. {#                  {% if not shop.acceptable %}#}
  36. {#                    <a href="{{ path('shop', {id: shop.id} )}}" class="shop_unacceptable">受付時間外です</a>#}
  37. {#                  {% endif %}#}
  38. {#                  <h3>{{ menu.name }}</h3>#}
  39. {#                  <div class="price">¥ {{ menu.price|number_format }} -</div>#}
  40. {#                </li>#}
  41. {#              {% endfor %}#}
  42. {#            </ul>#}
  43. {#          </div>#}
  44. {#          <div class="info_wr">#}
  45. {#            <a href="{{ path('shop', {id: shop.id} )}}" class="details_wr">#}
  46. {#              <h2>{{ shop.name }}</h2>#}
  47. {#              <div class="categories">{{ shop.categories|join(' / ') }}</div>#}
  48. {#              <ul class="provide">#}
  49. {#                {% if shop.toGo %}<li>テイクアウト</li>{% endif %}#}
  50. {#                {% if shop.delivery %}<li>デリバリー</li>{% endif %}#}
  51. {#                <li class="indication">提供時間目安 <span>{{ shop.estimatedMinutes|default('-') }}分</span></li>#}
  52. {#              </ul>#}
  53. {#            </a>#}
  54. {#          </div>#}
  55. {#        </div>#}
  56. {#        {% endfor %}#}
  57.       </div>
  58.     </section>
  59.   </main>
  60. {% endblock %}
  61. {% block js %}
  62.   <script async defer src="https://maps.googleapis.com/maps/api/js?language=ja&region=JP&key=AIzaSyACq30V5WAPj9fOOALRSbMp-sgnuY3N2I4&callback=initMap"></script>
  63.   <script type="text/javascript" src="//cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.min.js"></script>
  64.   <script>
  65.     var map;
  66.     var position = {lat: 36.69084630695481, lng: 137.21299807365202};
  67.     var slickOptions = {
  68.       variableWidth: true,
  69.       arrows: false,
  70.       dots: false
  71.     };
  72.     var bounds;
  73.     var pinCache = {};
  74.     var currentMarkers = [];
  75.     var fetchTimer;
  76.     $(function () {
  77.       // $('.slick-carousel').slick({
  78.       //   variableWidth: true,
  79.       //   arrows: false,
  80.       //   dots: false
  81.       // });
  82.       $('#geolocation').click(function () {
  83.         if (!navigator.geolocation) {
  84.           return false;
  85.         }
  86.         navigator.geolocation.getCurrentPosition(function (result) {
  87.           position = {lat: result.coords.latitude, lng: result.coords.longitude};
  88.           map.panTo(position);
  89.         });
  90.       });
  91.     });
  92.     // $.ajax({
  93.     //   url: 'https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDxtlmq6i3t7yKHPlFdgvRLZh2dN4XnEok',
  94.     //   method: 'post',
  95.     //   contentType: 'application/json',
  96.     //   data: {
  97.     //
  98.     //   },
  99.     //   crossDomain: true
  100.     // }).done(function(data) {
  101.     //   position = data.location;
  102.     //   if (map) {
  103.     //     map.setCenter(position);
  104.     //   }
  105.     // });
  106.     function loadPins() {
  107.       $.ajax({
  108.         url: '/pins?lat1=' + bounds.getSouthWest().lat() + '&lng1=' + bounds.getSouthWest().lng() + '&lat2=' + bounds.getNorthEast().lat() + '&lng2=' + bounds.getNorthEast().lng(),
  109.         type: 'json'
  110.       }).done(function (json) {
  111.         $.each(json, function (key, shop) {
  112.           if (!pinCache[shop.id]) {
  113.             pinCache[shop.id] = shop;
  114.             // Insert HTML
  115.             var $html = $(shop.html);
  116.             var marker = new google.maps.Marker({
  117.               position: {lat: parseFloat(shop.latitude), lng: parseFloat(shop.longitude)},
  118.               map: map,
  119.               draggable: false,
  120.               icon: {
  121.                 url: '/img/map-marker.png',
  122.                 scaledSize: new google.maps.Size(40, 40),
  123.                 size: new google.maps.Size(80, 80),
  124.                 origin: new google.maps.Point(0, 0),
  125.                 anchor: new google.maps.Point(20, 38)
  126.               }
  127.             });
  128.             marker.addListener('click', function() {
  129.               map.panTo(marker.getPosition());
  130.               $('.shop_wr').hide();
  131.               $html.show();
  132.               $('.footer-panel').toggleClass('open', true);
  133.             });
  134.             $html.appendTo($('#map-footer-container'));
  135.             // $('.slick-carousel').each(function () {
  136.             //   if (!$(this).is('.slick-initialized')) {
  137.             //     $(this).find('.food_img').each(function () {
  138.             //       $(this).css({
  139.             //         'background-image': 'url(' + $(this).data('img') + ')'
  140.             //       })
  141.             //     });
  142.             //     $(this).slick(slickOptions);
  143.             //   }
  144.             // });
  145.           }
  146.         });
  147.       });
  148.     }
  149.     function initMap() {
  150.       $(function () {
  151.         map = new google.maps.Map($('#map-container').get(0), {
  152.           center: position,
  153.           zoom: 16,
  154.           disableDefaultUI: true
  155.         });
  156.         map.addListener('click', function () {
  157.           $('.footer-panel').toggleClass('open', false);
  158.         });
  159.         // setTimeout(function () {
  160.         //   $('[data-shop]').each(function () {
  161.         //     var markerPosition = {lat: parseFloat($(this).data('lat')), lng: parseFloat($(this).data('lng'))};
  162.         //
  163.         //     var $panel = $(this);
  164.         //
  165.         //     var marker = new google.maps.Marker({
  166.         //       position: markerPosition,
  167.         //       map: map,
  168.         //       draggable: false,
  169.         //       icon: {
  170.         //         url: '/img/map-marker.png',
  171.         //         scaledSize: new google.maps.Size(40, 40),
  172.         //         size: new google.maps.Size(80, 80),
  173.         //         origin: new google.maps.Point(0, 0),
  174.         //         anchor: new google.maps.Point(20, 38)
  175.         //       }
  176.         //     });
  177.         //
  178.         //     marker.addListener('click', function() {
  179.         //       //console.log($panel);
  180.         //       var $carousel = $panel.find('.slick-carousel');
  181.         //       map.panTo(marker.getPosition());
  182.         //       $('.shop_wr').hide();
  183.         //       if (!$carousel.is('.slick-initialized')) {
  184.         //         $carousel.find('.food_img').each(function () {
  185.         //           $(this).css({
  186.         //             'background-image': 'url(' + $(this).data('img') + ')'
  187.         //           })
  188.         //         });
  189.         //         $carousel.slick(slickOptions);
  190.         //       }
  191.         //       $panel.show();
  192.         //       $('.footer-panel').toggleClass('open', true);
  193.         //     });
  194.         //
  195.         //   });
  196.         // }, 100);
  197.         bounds = map.getBounds();
  198.         map.addListener('bounds_changed', function () {
  199.           bounds = map.getBounds();
  200.           console.log(bounds.getCenter().toString());
  201.           //console.log(bounds.getNorthEast().toString(), bounds.getSouthWest().toString());
  202.           //loadPins();
  203.           if (fetchTimer) {
  204.             clearTimeout(fetchTimer);
  205.           }
  206.           fetchTimer = setTimeout(loadPins, 200);
  207.         });
  208.       });
  209.     }
  210.   </script>
  211. {% endblock %}