Skip to content

Commit 7a323b8

Browse files
authored
Create map.js
1 parent 1f51198 commit 7a323b8

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

assets/js/map.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
$(function() {
2+
// Asynchronously Load the map API
3+
var script = document.createElement('script');
4+
5+
script.src = "//maps.googleapis.com/maps/api/js?key=AIzaSyDwdfV6VtbmL1KnGSCQL1H2ZvGrr2eED7c&callback=initialize";
6+
document.body.appendChild(script);
7+
});
8+
9+
function initialize() {
10+
var map;
11+
var bounds = new google.maps.LatLngBounds();
12+
var mapOptions = {
13+
mapTypeId: 'roadmap'
14+
};
15+
16+
// Display a map on the page
17+
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
18+
map.setTilt(45);
19+
20+
// Multiple Markers
21+
var markers = [
22+
['London Eye, London', 51.503454,-0.119562],
23+
['Palace of Westminster, London', 51.499633,-0.124755],
24+
['London South Bank University', 51.501544,-0.122159]
25+
];
26+
27+
// Info Window Content
28+
var infoWindowContent = [
29+
['<div class="info_content">' +
30+
'<h3>London Eye</h3>' +
31+
'<p>The London Eye is a giant Ferris wheel situated on the banks of the River Thames. The entire structure is 135 metres (443 ft) tall and the wheel has a diameter of 120 metres (394 ft).</p>' + '</div>'],
32+
['<div class="info_content">' +
33+
'<h3>Palace of Westminster</h3>' +
34+
'<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' +
35+
'</div>'],
36+
['<div class="info_content">' +
37+
'<h3>London South Bank University</h3>' +
38+
'<p>The Palace of Westminster is the meeting place of the House of Commons and the House of Lords, the two houses of the Parliament of the United Kingdom. Commonly known as the Houses of Parliament after its tenants.</p>' +
39+
'</div>']
40+
];
41+
42+
// Display multiple markers on a map
43+
var infoWindow = new google.maps.InfoWindow(), marker, i;
44+
45+
// Loop through our array of markers & place each one on the map
46+
for( i = 0; i < markers.length; i++ ) {
47+
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
48+
bounds.extend(position);
49+
marker = new google.maps.Marker({
50+
position: position,
51+
map: map,
52+
title: markers[i][0]
53+
});
54+
55+
// Allow each marker to have an info window
56+
google.maps.event.addListener(marker, 'click', (function(marker, i) {
57+
return function() {
58+
infoWindow.setContent(infoWindowContent[i][0]);
59+
infoWindow.open(map, marker);
60+
}
61+
})(marker, i));
62+
63+
// Automatically center the map fitting all markers on the screen
64+
map.fitBounds(bounds);
65+
}
66+
67+
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
68+
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
69+
this.setZoom(14);
70+
google.maps.event.removeListener(boundsListener);
71+
});
72+
73+
}

0 commit comments

Comments
 (0)