article

Wednesday, December 18, 2019

Multiple Markers with Info Windows to Google Maps

Multiple Markers with Info Windows to Google Maps
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiple Markers with Info Windows to Google Maps</title>
<style type="text/css">
#mapCanvas {
    width: 100%;
    height: 450px;
}
body {font-family: "Times New Roman", Times, serif;}
</style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-lg-12">
   <p><h3>Multiple Markers with Info Windows to Google Maps</h3></p>
   <div id="mapContainer">
                <div id="mapCanvas"></div>
            </div>
     </div>
   </div>
</div>
<script>
function initMap() {
    var map;
    var bounds = new google.maps.LatLngBounds();
    var mapOptions = {
        mapTypeId: 'roadmap'
    };
                     
    // Display a map on the web page
    map = new google.maps.Map(document.getElementById("mapCanvas"), mapOptions);
    map.setTilt(50);
         
    // Multiple markers location, latitude, and longitude
    var markers = [
        ['Olongapo City, Zambales', 14.841344, 120.282224],
        ['Subic Bay, Zambales', 14.786400, 120.281535],
        ['Angeles City, Pampanga', 15.145334, 120.585616],
        ['Clark Freeportzone, Pampanga', 15.185247, 120.539395]
    ];
                         
    // Info window content
    var infoWindowContent = [
        ['<div class="info_content">' +
        '<h3>Olongapo City, Philippines</h3>' +
        '<p>Olongapo City Zambales Philippines</p>' + '</div>'],
        ['<div class="info_content">' +
        '<h3>Subic Bay, Philippines</h3>' +
        '<p>Subic Bay Olongapo City Zambales Philippines</p>' +
        '</div>'],
        ['<div class="info_content">' +
        '<h3>Angeles City, Pampanga</h3>' +
        '<p>Angeles City, Pampanga Philippines</p>' +
        '</div>'],
        ['<div class="info_content">' +
        '<h3>Clark Freeportzone, Pampanga</h3>' +
        '<p>Clark Freeportzone, Pampanga Philippines</p>' +
        '</div>']
    ];
         
    // Add multiple markers to map
    var infoWindow = new google.maps.InfoWindow(), marker, i;
     
    // Place each marker on the map 
    for( i = 0; i < markers.length; i++ ) {
        var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
        bounds.extend(position);
        marker = new google.maps.Marker({
            position: position,
            map: map,
            title: markers[i][0]
        });
         
        // Add info window to marker   
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infoWindow.setContent(infoWindowContent[i][0]);
                infoWindow.open(map, marker);
            }
        })(marker, i));
 
        // Center the map to fit all markers on the screen
        map.fitBounds(bounds);
    }
 
    // Set zoom level
    var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
        this.setZoom(9);
        google.maps.event.removeListener(boundsListener);
    });
     
}
// Load initialize function
google.maps.event.addDomListener(window, 'load', initMap);
</script>
</body>
</html>

Related Post