templates/public/prayer/_map.js.twig line 1

Open in your IDE?
  1. (function ($) {
  2.     "use strict";
  3.     function mainMap(locations) {
  4.         //   Map Infoboxes end ------------------
  5.         var map = new google.maps.Map(document.getElementById('map-main'), {
  6.             zoom: 6,
  7.             scrollwheel: false,
  8.             center: new google.maps.LatLng(-18, 47),
  9.             mapTypeId: google.maps.MapTypeId.ROADMAP,
  10.             zoomControl: false,
  11.             mapTypeControl: false,
  12.             scaleControl: false,
  13.             panControl: false,
  14.             fullscreenControl: true,
  15.             navigationControl: false,
  16.             streetViewControl: false,
  17.             animation: google.maps.Animation.BOUNCE,
  18.             gestureHandling: 'cooperative',
  19.             styles: [{
  20.                 "featureType": "administrative",
  21.                 "elementType": "labels.text.fill",
  22.                 "stylers": [{
  23.                     "color": "#444444"
  24.                 }]
  25.             }]
  26.         });
  27.         var boxText = document.createElement("div");
  28.         boxText.className = 'map-box'
  29.         var currentInfobox;
  30.         var boxOptions = {
  31.             content: boxText,
  32.             disableAutoPan: true,
  33.             alignBottom: true,
  34.             maxWidth: 0,
  35.             pixelOffset: new google.maps.Size(-150, -55),
  36.             zIndex: null,
  37.             boxStyle: {
  38.                 width: "300px"
  39.             },
  40.             closeBoxMargin: "0",
  41.             closeBoxURL: "",
  42.             infoBoxClearance: new google.maps.Size(1, 1),
  43.             isHidden: false,
  44.             pane: "floatPane",
  45.             enableEventPropagation: false,
  46.         };
  47.         var markerCluster, overlay, i;
  48.         var allMarkers = [];
  49.         var clusterStyles = [
  50.             {
  51.                 textColor: 'white',
  52.                 url: '',
  53.                 height: 50,
  54.                 width: 50
  55.             }
  56.         ];
  57.         var ib = new InfoBox();
  58.         google.maps.event.addListener(ib, "domready", function () {
  59.             cardRaining();
  60.         });
  61.         var markerImg;
  62.         var markerCount;
  63.         $.each(locations, function(index, user) {
  64.             var newLocation = generateRandomCordonateInRadius({'latitude':user.latitude, 'longitude':user.longitude} , 1000) ;
  65.             var labels = '123456789';
  66.             markerImg = user.image;
  67.             markerCount = user.nb_prayings;
  68.             var overlaypositions = new google.maps.LatLng(newLocation.latitude, newLocation.longitude),
  69.                 overlay = new CustomMarker(
  70.                     overlaypositions, map,{ marker_id: index},  markerImg , markerCount
  71.                 );
  72.             allMarkers.push(overlay);
  73.             google.maps.event.addDomListener(overlay, 'click', (function(overlay, index) {
  74.                 return function() {
  75.                     ib.setOptions(boxOptions);
  76.                     boxText.innerHTML = user.popupInfo;
  77.                     ib.close();
  78.                     ib.open(map, overlay);
  79.                     currentInfobox = user.nb_prayings;
  80.                     var latLng = new google.maps.LatLng(newLocation.latitude, newLocation.longitude);
  81.                     map.panTo(latLng);
  82.                     map.panBy(0, -110);
  83.                     google.maps.event.addListener(ib,'domready',function(){
  84.                         $('.infoBox-close').click(function(e) {
  85.                             e.preventDefault();
  86.                             ib.close();
  87.                             $('.map-marker-container').removeClass('clicked infoBox-opened');
  88.                         });
  89.                     });
  90.                 }
  91.             })(overlay, i));});
  92.         var options2 = {
  93.             imagePath: '',
  94.             styles: clusterStyles,
  95.             minClusterSize: 2
  96.         };
  97.         markerCluster = new MarkerClusterer(map, allMarkers, options2);
  98.         google.maps.event.addDomListener(window, "resize", function () {
  99.             var center = map.getCenter();
  100.             google.maps.event.trigger(map, "resize");
  101.             map.setCenter(center);
  102.         });
  103.         $('.map-item').on("click", function (e) {
  104.             e.preventDefault();
  105.             map.setZoom(15);
  106.             var index = currentInfobox;
  107.             var marker_index = parseInt($(this).attr('href').split('#')[1], 10);
  108.             google.maps.event.trigger(allMarkers[marker_index-1], "click");
  109.             if ($(window).width() > 1064) {
  110.                 if ($(".map-container").hasClass("fw-map")) {
  111.                     $('html, body').animate({
  112.                         scrollTop: $(".map-container").offset().top + "-110px"
  113.                     }, 1000)
  114.                     return false;
  115.                 }
  116.             }
  117.         });
  118.         $('.nextmap-nav').on("click", function (e) {
  119.             e.preventDefault();
  120.             map.setZoom(15);
  121.             var index = currentInfobox;
  122.             if (index + 1 < allMarkers.length) {
  123.                 google.maps.event.trigger(allMarkers[index+ 1], 'click');
  124.             } else {
  125.                 google.maps.event.trigger(allMarkers[0], 'click');
  126.             }
  127.         });
  128.         $('.prevmap-nav').on("click", function (e) {
  129.             e.preventDefault();
  130.             map.setZoom(15);
  131.             if (typeof (currentInfobox) == "undefined") {
  132.                 google.maps.event.trigger(allMarkers[allMarkers.length - 1], 'click');
  133.             } else {
  134.                 var index = currentInfobox;
  135.                 if (index - 1 < 0) {
  136.                     google.maps.event.trigger(allMarkers[allMarkers.length - 1], 'click');
  137.                 } else {
  138.                     google.maps.event.trigger(allMarkers[index - 1], 'click');
  139.                 }
  140.             }
  141.         });
  142.         // Scroll enabling button
  143.         var scrollEnabling = $('.scrollContorl');
  144.         $(scrollEnabling).click(function(e){
  145.             e.preventDefault();
  146.             $(this).toggleClass("enabledsroll");
  147.             if ( $(this).is(".enabledsroll") ) {
  148.                 map.setOptions({'scrollwheel': true});
  149.             } else {
  150.                 map.setOptions({'scrollwheel': false});
  151.             }
  152.         });
  153.         var zoomControlDiv = document.createElement('div');
  154.         var zoomControl = new ZoomControl(zoomControlDiv, map);
  155.         function ZoomControl(controlDiv, map) {
  156.             zoomControlDiv.index = 1;
  157.             map.controls[google.maps.ControlPosition.RIGHT_CENTER].push(zoomControlDiv);
  158.             controlDiv.style.padding = '5px';
  159.             var controlWrapper = document.createElement('div');
  160.             controlDiv.appendChild(controlWrapper);
  161.             var zoomInButton = document.createElement('div');
  162.             zoomInButton.className = "mapzoom-in";
  163.             controlWrapper.appendChild(zoomInButton);
  164.             var zoomOutButton = document.createElement('div');
  165.             zoomOutButton.className = "mapzoom-out";
  166.             controlWrapper.appendChild(zoomOutButton);
  167.             google.maps.event.addDomListener(zoomInButton, 'click', function () {
  168.                 map.setZoom(map.getZoom() + 1);
  169.             });
  170.             google.maps.event.addDomListener(zoomOutButton, 'click', function () {
  171.                 map.setZoom(map.getZoom() - 1);
  172.             });
  173.         }
  174.         // Geo Location Button
  175.         $(".geoLocation, .input-with-icon.location a").on("click", function (e) {
  176.             e.preventDefault();
  177.             geolocate();
  178.         });
  179.         function geolocate() {
  180.             if (navigator.geolocation) {
  181.                 navigator.geolocation.getCurrentPosition(function (position) {
  182.                     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
  183.                     map.setCenter(pos);
  184.                     map.setZoom(12);
  185.                     var avrtimg = $(".avatar-img").attr("data-srcav");
  186.                     var markerIcon3 = {
  187.                         url: avrtimg,
  188.                     }
  189.                     var marker3 = new google.maps.Marker({
  190.                         position: pos,
  191.                         map:  map,
  192.                         icon: markerIcon3,
  193.                         title: 'Your location'
  194.                     });
  195.                     var myoverlay = new google.maps.OverlayView();
  196.                     myoverlay.draw = function () {
  197.                         // add an id to the layer that includes all the markers so you can use it in CSS
  198.                         this.getPanes().markerLayer.id='markerLayer';
  199.                     };
  200.                     myoverlay.setMap(map);
  201.                 });
  202.             }
  203.         }
  204.     }
  205.     /**
  206.      * radius in meters
  207.      */
  208.     function generateRandomCordonateInRadius(position, radius) {
  209.         var y0 = position.latitude;
  210.         var x0 = position.longitude;
  211.         //1 degree = 111300 meters
  212.         // Convert Radius from meters to degrees.
  213.         var rd = radius / 111300;
  214.         var u = Math.random();
  215.         var v = Math.random();
  216.         var w = rd * Math.sqrt(u);
  217.         var t = 2 * Math.PI * v;
  218.         var x = w * Math.cos(t);
  219.         var y = w * Math.sin(t);
  220.         var xp = x / Math.cos(y0);
  221.         return {
  222.             'latitude': y + y0,
  223.             'longitude': xp + x0
  224.         };
  225.     }
  226.     // Custom Map Marker
  227.     // ----------------------------------------------- //
  228.     function CustomMarker(latlng, map, args, markerImg, markerCount) {
  229.         this.latlng = latlng;
  230.         this.args = args;
  231.         this.markerImg = markerImg;
  232.         this.markerCount = markerCount;
  233.         this.setMap(map);
  234.     }
  235.     CustomMarker.prototype = new google.maps.OverlayView();
  236.     CustomMarker.prototype.draw = function () {
  237.         var self = this;
  238.         var div = this.div;
  239.         if (!div) {
  240.             var markerimage = "{{ url('asset_url', {'path': '/images/logo.png', 'w':'60' }) }}";
  241.             div = this.div = document.createElement('div');
  242.             div.className = 'map-marker-container';
  243.             {#'<span class="marker-count">' + self.markerCount + '</span>' +#}
  244.             div.innerHTML = '<div class="marker-container">' +
  245.                 '<div class="marker-card">' +
  246.                 '<div class="marker-holder"><img src="' + markerimage + '" alt=""></div>' +
  247.                 '</div>' +
  248.                 '</div>'
  249.             // Clicked marker highlight
  250.             google.maps.event.addDomListener(div, "click", function (event) {
  251.                 $('.map-marker-container').removeClass('clicked infoBox-opened');
  252.                 google.maps.event.trigger(self, "click");
  253.                 $(this).addClass('clicked infoBox-opened');
  254.             });
  255.             if (typeof (self.args.marker_id) !== 'undefined') {
  256.                 div.dataset.marker_id = self.args.marker_id;
  257.             }
  258.             var panes = this.getPanes();
  259.             panes.overlayImage.appendChild(div);
  260.         }
  261.         var point = this.getProjection().fromLatLngToDivPixel(this.latlng);
  262.         if (point) {
  263.             div.style.left = (point.x) + 'px';
  264.             div.style.top = (point.y) + 'px';
  265.         }
  266.     };
  267.     CustomMarker.prototype.remove = function () {
  268.         if (this.div) {
  269.             this.div.parentNode.removeChild(this.div);
  270.             this.div = null;
  271.             $(this).removeClass('clicked');
  272.         }
  273.     };
  274.     CustomMarker.prototype.getPosition = function () {
  275.         return this.latlng;
  276.     };
  277.     // -------------- Custom Map Marker / End -------------- //
  278.     var head = document.getElementsByTagName('head')[0];
  279. // Save the original method
  280.     var insertBefore = head.insertBefore;
  281. // Replace it!
  282.     head.insertBefore = function (newElement, referenceElement) {
  283.         if (newElement.href && newElement.href.indexOf('https://fonts.googleapis.com/css?family=Roboto') === 0) {
  284.             return;
  285.         }
  286.         insertBefore.call(head, newElement, referenceElement);
  287.     };
  288.     var map = document.getElementById('map-main');
  289.     if (typeof (map) != 'undefined' && map != null) {
  290.         google.maps.event.addDomListener(window, 'load', function () {
  291.             $.ajax({
  292.                 url: "{{ path('praying_users') }}",
  293.                 type: "GET",
  294.                 success: function (users) {
  295.                     mainMap(users);
  296.                 }
  297.             })
  298.         });
  299.     }
  300. })(this.jQuery);