-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (161 loc) · 7.03 KB
/
script.js
File metadata and controls
178 lines (161 loc) · 7.03 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// initialize the variables we need
// we do this here to make sure we can access them
// whenever we need to -- they have 'global scope'
var my_map; // this will hold the map
var my_map_options; // this will hold the options we'll use to create the map
var my_center = new google.maps.LatLng(41.8986,12.4768); // center of map
var my_markers = []; // we use this in the main loop below to hold the markers
// this one is strange. In google maps, there is usually only one
// infowindow object -- its content and position change when you click on a
// marker. This is counterintuitive, but we need to live with it.
var infowindow = new google.maps.InfoWindow({content: ""});
var legendHTML = "<h1>Legend</h1>";
// I'm complicating things a bit with this next set of variables, which will help us
// to make multi-colored markers
var blueURL = "http://maps.google.com/mapfiles/ms/icons/blue-dot.png";
var redURL = "http://maps.google.com/mapfiles/ms/icons/red-dot.png";
var red_markers = [];
var blue_markers = [];
// this is for fun, if you want it. With this powerful feature you can add arbitrary
// data layers to your map. It's cool. Learn more at:
// https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson
var myGeoJSON= {
"type":"FeatureCollection",
"features":
[{"type":"Feature",
"properties":{myColor: 'red'},
"myColor" : "red",
"geometry":{"type":"Polygon",
"coordinates":[[[-85.60546875,49.03786794532644],[-96.6796875,40.713955826286046],
[-79.62890625,37.71859032558816],[-81.2109375,49.26780455063753],
[-85.60546875,49.03786794532644]]]}},
{"type":"Feature",
"properties":{myColor: 'green'},
"myColor" : "green",
"geometry":{"type":"Polygon",
"coordinates":[[[-113.203125,58.35563036280967],[-114.78515624999999,51.944264879028765],
[-101.6015625,51.944264879028765],[-112.32421875,58.263287052486035],
[-113.203125,58.35563036280967]]]
}}]};
/* a function that will run when the page loads. It creates the map
and the initial marker. If you want to create more markers, do it here. */
function initializeMap() {
my_map_options = {
center: my_center, // to change this value, change my_center above
zoom: 13, // higher is closer-up
mapTypeId: google.maps.MapTypeId.HYBRID // you can also use TERRAIN, STREETMAP, SATELLITE
};
// this one line creates the actual map
my_map = new google.maps.Map(document.getElementById("map_canvas"),
my_map_options);
// this is an *array* that holds all the marker info
var all_my_markers =
[{position: new google.maps.LatLng(41.9000,12.5000),
map: my_map,
icon: blueURL, // this sets the image that represents the marker in the map to the one
// located at the URL which is given by the variable blueURL, see above
title: "first Marker",
window_content: "<h1>Marker1</h1><p> and this would be the extended description</p>"
},
{position: new google.maps.LatLng(41.8902,12.4923),
map: my_map,
icon: blueURL, // this sets the image that represents the marker in the map
title: "second Marker",
window_content: "<h1>Marker2</h1><p> and <a href='http://something'>this would</a> be the extended description</p>"
},
{position: new google.maps.LatLng(41.8986,12.4768),
map: my_map,
icon: redURL, // this sets the image that represents the marker in the map
title: "third Marker",
window_content: '<h1>Marker3</h1><img title="Picture of Quote. Src: someone, some year" src="https://s-media-cache-ak0.pinimg.com/736x/6d/e2/25/6de2251b8b4be709dcc936ae4f0caaaf.jpg"/>' +
'<blockquote>quote quote quote quote</blockquote>'
}
];
for (j = 0; j < all_my_markers.length; j++) {
var marker = new google.maps.Marker({
position: all_my_markers[j].position,
map: my_map,
icon: all_my_markers[j].icon,
title: all_my_markers[j].title,
window_content: all_my_markers[j].window_content});
// this next line is ugly, and you should change it to be prettier.
// be careful not to introduce syntax errors though.
legendHTML +=
"<div class=\"pointer\" onclick=\"locateMarker(my_markers[" + j + "])\"> " +
marker.window_content + "</div>";
marker.info = new google.maps.InfoWindow({content: marker.window_content});
var listener = google.maps.event.addListener(marker, 'click', function() {
// if you want to allow multiple info windows, uncomment the next line
// and comment out the two lines that follow it
//this.info.open(this.map, this);
infowindow.setContent (this.window_content);
infowindow.open(my_map, this);
});
my_markers.push({marker:marker, listener:listener});
if (all_my_markers[j].icon == blueURL ) {
blue_markers.push({marker:marker, listener:listener});
} else if (all_my_markers[j].icon == redURL ) {
red_markers.push({marker:marker, listener:listener});
}
}
document.getElementById("map_legend").innerHTML = legendHTML;
my_map.data.addGeoJson(myGeoJSON);
var romeCircle = new google.maps.Rectangle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: my_map,
bounds: {
north: 42.685,
south: 40.671,
east: 12.501,
west: 12.485
},
center: {"lat": 41.9000, "lng":12.5000},
radius: 1000
});
my_map.data.setStyle(function (feature) {
var thisColor = feature.getProperty("myColor");
return {
fillColor: thisColor,
strokeColor: thisColor,
strokeWeight: 5
};
});
}
// this hides all markers in the array
// passed to it, by attaching them to
// an empty object (instead of a real map)
function hideMarkers (marker_array) {
for (var j in marker_array) {
marker_array[j].marker.setMap(null);
}
}
// by contrast, this attaches all the markers to
// a real map object, so they reappear
function showMarkers (marker_array, map) {
for (var j in marker_array) {
marker_array[j].marker.setMap(map);
}
}
//global variable to track state of markers
var markersHidden = false;
function toggleMarkers (marker_array, map) {
for (var j in marker_array) {
if (markersHidden) {
marker_array[j].marker.setMap(map);
} else {
marker_array[j].marker.setMap(null);
}
}
markersHidden = !markersHidden;
}
// I added this for fun. It allows you to trigger the infowindow
// form outside the map.
function locateMarker (marker) {
console.log(marker);
my_map.panTo(marker.marker.position);
google.maps.event.trigger(marker.marker, 'click');
}