Willcode4cash

Google Maps w/multiple markers Pt. 2

Jun 6th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.28 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>Google Maps Multiple Markers</title>
  5.   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  6.   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" type="text/javascript"></script>
  7.   <script src="http://maps.google.com/maps/api/js?key=<INSERT YOUR OWN DAMN KEY>" type="text/javascript"></script>
  8.   <script type="text/javascript">
  9.     var map;
  10.     var centerLatLng = new google.maps.LatLng(-33.92, 151.25);
  11.  
  12.     // Create the map, define zoom level, set center point and declare map type
  13.     function initialize() {
  14.       map = new google.maps.Map(document.getElementById('map'), {
  15.         zoom: 10,
  16.         center: centerLatLng,
  17.         mapTypeId: google.maps.MapTypeId.HYBRID
  18.       });
  19.  
  20.       // When the map container is re-sized, reset the center point of the map
  21.       google.maps.event.addDomListenerOnce(map, 'idle', function () {
  22.         google.maps.event.addDomListener(window, 'resize', function () {
  23.             map.setCenter(centerLatLng);
  24.         });
  25.       });
  26.     }
  27.  
  28.     // Accepts the array of markers and places those markers, with titles on the map.
  29.     function loadMarkers(locations) {
  30.       for (var i = 0; i < locations.length; i++) {  
  31.        new google.maps.Marker({
  32.          position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  33.          map: map,
  34.          title: locations[i][0]
  35.        });
  36.      }
  37.    }
  38.  
  39.    // Once the page is finshed loading ...
  40.    $(document).ready(function() {
  41.      // Initialize the map,
  42.      initialize();
  43.  
  44.      // Load the markers to the map. Since the markers are loaded after the page is
  45.      // loaded, the marker data can be loaded in the view.
  46.      loadMarkers([
  47.          ['Bondi Beach', -33.890542, 151.274856],
  48.          ['Coogee Beach', -33.923036, 151.259052],
  49.          ['Cronulla Beach', -34.028249, 151.157507],
  50.          ['Manly Beach', -33.800101, 151.287478],
  51.          ['Maroubra Beach', -33.950198, 151.259302]
  52.        ]);
  53.    });
  54.  </script>
  55.   <style>
  56.       body {
  57.         margin: 0;
  58.         padding: 0;
  59.       }
  60.  
  61.       #map {
  62.         margin: 5px;
  63.         border: 1px solid #333;
  64.         height: 300px;
  65.       }
  66.   </style>
  67. </head>
  68. <body>
  69.   <div id="map" name="map"></div>
  70. </body>
  71. </html>
Add Comment
Please, Sign In to add comment