-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathgoogle_map.js
More file actions
157 lines (130 loc) · 3.75 KB
/
google_map.js
File metadata and controls
157 lines (130 loc) · 3.75 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
$(function(){
// If a container for a Google Map exists on this page...
var canvas = $("#google_map");
if (canvas[0]){
// Setup the custom map markers.
var markerImage = new google.maps.MarkerImage(
"/images/map-marker.png",
new google.maps.Size(22,35),
null,
new google.maps.Point(11, 34)
);
var markerHover = new google.maps.MarkerImage(
"/images/map-marker.png",
new google.maps.Size(22,35),
new google.maps.Point(22, 0),
new google.maps.Point(11, 34)
);
var markerShadow = new google.maps.MarkerImage(
"/images/map-marker.png",
new google.maps.Size(27,35),
new google.maps.Point(44, 0),
new google.maps.Point(3, 30)
);
// Parse city data into objects.
var cities = [];
$("ul#cities li.city").each(function(){
var a, li;
li = $(this);
a = $(this).find("a");
cities.push({
name: a.text(),
id: li.attr("id"),
url: a.attr("href"),
lat: parseFloat(li.attr("data-latitude")),
lng: parseFloat(li.attr("data-longitude"))
});
})
// Add the Google Map to the page.
canvas.addClass("js");
var map = new google.maps.Map(canvas[0], {
streetViewControl: false,
mapTypeControl: false,
zoomControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// For each city, create a marker and adjust the map bounds.
var markers = [];
var bounds = new google.maps.LatLngBounds();
for (i in cities){
var city = cities[i];
var latlng = new google.maps.LatLng(city.lat,city.lng);
var marker = new google.maps.Marker({
position: latlng,
icon: markerImage,
shadow: markerShadow
});
markers.push(marker);
marker.setMap(map);
bounds.extend(latlng);
}
// Created a city/state label for each marker.
var labels = [];
var labelPrefix = "<div class='map_label'><div><p><span>";
var labelSuffix = "</span></p></div></div>";
for (i in markers){
labels.push(new InfoBox({
content: labelPrefix + cities[i].name + labelSuffix,
closeBoxURL: "",
disableAutoPan: true,
boxStyle: {
width: "0",
height: "0",
overflow: "visible"
}
}));
}
// Fit the map to show all of the cities.
// Also do this whenever the browser is resized.
$(window).resize(function() {
map.fitBounds(bounds);
});
$(window).resize();
// Highlight a map marker whenever the corresponding list item is hovered.
var links = $("ul#cities li.city a");
links.hover(function(){
var link = $(this);
var index = links.index(link);
markers[index].setIcon(markerHover);
}, function(){
var link = $(this);
var index = links.index(link);
markers[index].setIcon(markerImage);
});
// When a map marker is hovered over...
// - highlight the corresponding list item
// - display a label over the marker
for (i in markers){
google.maps.event.addListener(markers[i], 'mouseover', function() {
var i = markers.indexOf(this);
this.setIcon(markerHover);
links.eq(i).addClass("highlight");
labels[i].open(map, markers[i]);
});
google.maps.event.addListener(markers[i], 'mouseout', function() {
var i = markers.indexOf(this);
this.setIcon(markerImage);
links.eq(i).removeClass("highlight");
labels[i].close();
});
}
// Click on a map marker to navigate to the page for that city.
for (i in markers){
google.maps.event.addListener(markers[i], 'click', function() {
window.location = cities[(markers.indexOf(this))].url
});
}
// Cluster map markers so that no two markers are overlapping.
var mc = new MarkerClusterer(map, markers, {
styles: [{
height: 48,
width: 48,
anchorIcon: [24, 24],
url: "/images/map-cluster.png",
textColor: "#fff",
textSize: 17,
fontFamily: "Helvetica"
}]
});
}
});