Willcode4cash

Google maps with multiple markers and info windows

May 4th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.46 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style>
  4.     body { padding: 0; margin: 0;}
  5.     h3 { padding: 0; margin: 0; }
  6.     #map_wrapper { height: 100%; }
  7.     #map_canvas { width: 100%; height: 100%; }
  8. </style>
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
  10. <script src="https://maps.googleapis.com/maps/api/js?sensor=false&callback=initialize" async defer></script>
  11. <script>
  12.     function initialize() {
  13.         var map;
  14.         var bounds = new google.maps.LatLngBounds();
  15.         var mapOptions = {
  16.             mapTypeId: 'hybrid',
  17.             center: {lat: 40.730610, lng: -73.935242}
  18.         };
  19.                        
  20.         map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
  21.         map.setTilt(45);
  22.            
  23.         // Define multiple markers
  24.         var markers = [
  25.             ['Times Square, Manhattan, NY', 40.758896, -73.985130],
  26.             ['Central Park, New York, NY', 40.785091, -73.968285],
  27.             ['Empire State Building, New York, NY', 40.748817, -73.985428]
  28.         ];
  29.  
  30.         // Define info window content
  31.         var infoWindowContent = [
  32.             ['<div class="info_content"><h3>Times Square, Manhattan, NY</h3><p>Times Square is a famous central square in Theater District of Manhattan, New York. It is located at the junction of Boradway and the Seventh Avenue. It appeared in the beginning of the last century and witnessed plenty of historic events. There are plenty of famous establishments located at the square, including Madame Tussauds Wax Museum, The Hard Rock Cafe New York, the Paramount Building, etc.</p></div>'],
  33.             ['<div class="info_content"><h3>Central Park, New York, NY</h3><p>One of the key landmarks of New Your City and Manhattan, Central Park is one of the most famous parks in the world. It appeared in many movies and plays an important role in local and American culture as a National Historic Site. The park is open year round and it is estimated that every year over 37 million people visit this amazing place.</p></div>'],
  34.             ['<div class="info_content"><h3>Empire State Building, New York, NY</h3><p>The Empire State Building is one of the world\'s most famous skyscraper located at 350 5th Avenue, Manhattan, New York. This building is given the name to the state, "The Empire State". It used to be the tallest buildings in New York after September 11, but now One World Trade Center is considered the tallest building of the city.</p></div>']
  35.         ];
  36.            
  37.         // Display multiple markers on a map
  38.         var infoWindow = new google.maps.InfoWindow(), marker, i;
  39.        
  40.         // Loop through our array of markers & place each one on the map  
  41.         for( i = 0; i < markers.length; i++ ) {
  42.             var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
  43.             bounds.extend(position);
  44.             marker = new google.maps.Marker({
  45.                 position: position,
  46.                 map: map,
  47.                 title: markers[i][0]
  48.             });
  49.            
  50.             // Allow each marker to have an info window    
  51.             google.maps.event.addListener(marker, 'click', (function(marker, i) {
  52.                 return function() {
  53.                     infoWindow.setContent(infoWindowContent[i][0]);
  54.                     infoWindow.open(map, marker);
  55.                 }
  56.             })(marker, i));
  57.  
  58.             // Automatically center the map fitting all markers on the screen
  59.             map.fitBounds(bounds);
  60.         }
  61.  
  62.         // Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
  63.         var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
  64.             this.setZoom(14);
  65.             google.maps.event.removeListener(boundsListener);
  66.         });
  67.     }
  68. </script>
  69. </head>
  70. <body>
  71. <div id="map_wrapper"><div id="map_canvas" class="mapping"></div></div>
  72. </body>
  73. </html>
Add Comment
Please, Sign In to add comment