yudaduy

Custom Marker + Marker Clustering

Sep 20th, 2021
2,435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.30 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title>Custom Marker</title>
  5.     <style>
  6.         /* Always set the map height explicitly to define the size of the div
  7.        * element that contains the map. */
  8.         #map {
  9.         height: 100%;
  10.         }
  11.  
  12.         /* Optional: Makes the sample page fill the window. */
  13.         html,
  14.         body {
  15.         height: 100%;
  16.         margin: 0;
  17.         padding: 0;
  18.         }
  19.     </style>
  20.  
  21.     <!-- jsFiddle will insert css and js -->
  22.   </head>
  23.   <body>
  24.     <div id="map"></div>
  25.  
  26.     <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
  27.     <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  28.     <script src="https://unpkg.com/@google/[email protected]/dist/markerclustererplus.min.js"></script>
  29.     <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap&v=weekly&channel=2" async></script>
  30.  
  31.     <script>
  32.  
  33.   function initMap() {
  34.     const map = new google.maps.Map(document.getElementById("map"), {
  35.       zoom: 4,
  36.       center: { lat: -28.024, lng: 140.887 },
  37.     });
  38.  
  39.     var infowindow = new google.maps.InfoWindow();
  40.  
  41.     const markers = locations.map((location, i) => {
  42.       var marker= new google.maps.Marker({
  43.         position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  44.         icon: locations[i][3]
  45.       });
  46.  
  47.       google.maps.event.addListener(marker, 'click', (function(marker, i) {
  48.           return function() {
  49.             infowindow.setContent(locations[i][0]);
  50.             infowindow.open(map, marker);
  51.           }
  52.         })(marker, i));
  53.         return marker;
  54.     });
  55.  
  56.  
  57.     // Add a marker clusterer to manage the markers.
  58.     new MarkerClusterer(map, markers, {
  59.       imagePath:
  60.         "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
  61.     });
  62.   }
  63.  
  64.   const locations = [
  65.     ['Headquarter',-31.56391,147.154312,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
  66.     ['Branch Store',-33.718234,150.363181,'http://labs.google.com/ridefinder/images/mm_20_yellow.png' ],
  67.     ['Branch Store',-33.727111,150.371124,'http://labs.google.com/ridefinder/images/mm_20_yellow.png' ],
  68.     ['Branch Store',-33.848588,151.209834,'http://labs.google.com/ridefinder/images/mm_20_yellow.png' ],
  69.     ['Branch Store',-33.851702,151.216968,'http://labs.google.com/ridefinder/images/mm_20_yellow.png' ],
  70.     ['Branch Store',-34.671264,150.863657,'http://labs.google.com/ridefinder/images/mm_20_yellow.png' ],
  71.     ['Branch Store',-35.304724,148.662905,'http://labs.google.com/ridefinder/images/mm_20_green.png' ],
  72.     ['Branch Store',-36.817685,175.699196,'http://labs.google.com/ridefinder/images/mm_20_green.png' ],
  73.     ['Branch Store',-36.828611,175.790222,'http://labs.google.com/ridefinder/images/mm_20_green.png' ],
  74.     ['Branch Store',-37.75,145.116667,'http://labs.google.com/ridefinder/images/mm_20_green.png' ],
  75.     ['Branch Store',-37.759859,145.128708,'http://labs.google.com/ridefinder/images/mm_20_green.png' ],
  76.     ['Branch Store',-37.765015,145.133858,'http://labs.google.com/ridefinder/images/mm_20_blue.png' ],
  77.     ['Branch Store',-37.770104,145.143299,'http://labs.google.com/ridefinder/images/mm_20_blue.png' ],
  78.     ['Branch Store',-37.7737,145.145187,'http://labs.google.com/ridefinder/images/mm_20_blue.png' ],
  79.     ['Branch Store',-37.774785,145.137978,'http://labs.google.com/ridefinder/images/mm_20_blue.png' ],
  80.     ['Branch Store',-37.819616,144.968119,'http://labs.google.com/ridefinder/images/mm_20_blue.png' ],
  81.     ['Branch Store',-38.330766,144.695692,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  82.     ['Branch Store',-39.927193,175.053218,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  83.     ['Branch Store',-41.330162,174.865694,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  84.     ['Branch Store',-42.734358,147.439506,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  85.     ['Branch Store',-42.734358,147.501315,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  86.     ['Branch Store',-42.735258,147.438,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  87.     ['Branch Store',-43.999792,170.463352,'http://labs.google.com/ridefinder/images/mm_20_purple.png' ],
  88.   ];
  89.     </script>
  90.   </body>
  91. </html>
Advertisement
Add Comment
Please, Sign In to add comment