-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap.js
More file actions
347 lines (304 loc) · 11.3 KB
/
map.js
File metadata and controls
347 lines (304 loc) · 11.3 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*jshint loopfunc: true */
var map;
// Create a new blank array for all the listing markers.
var markers = [];
var locations = [];
var test = [];
function initMap() {
var styles = [
{
featureType: 'water',
stylers: [
{ color: '#6A8194' }
]
},{
featureType: 'administrative',
elementType: 'labels.text.stroke',
stylers: [
{ color: '#ffffff' },
{ weight: 6 }
]
},{
featureType: 'administrative',
elementType: 'labels.text.fill',
stylers: [
{ color: '#1C262F' }
]
},{
featureType: 'road.highway',
elementType: 'geometry.stroke',
stylers: [
{ color: '#efe9e4' },
{ lightness: -40 }
]
},{
featureType: 'transit.station',
stylers: [
{ weight: 9 },
{ hue: '#e85113' }
]
},{
featureType: 'road.highway',
elementType: 'labels.icon',
stylers: [
{ visibility: 'off' }
]
},{
featureType: 'water',
elementType: 'labels.text.stroke',
stylers: [
{ lightness: 100 }
]
},{
featureType: 'water',
elementType: 'labels.text.fill',
stylers: [
{ lightness: -100 }
]
},{
featureType: 'poi',
elementType: 'geometry',
stylers: [
{ visibility: 'on' },
{ color: '#f0e4d3' }
]
},{
featureType: 'road.highway',
elementType: 'geometry.fill',
stylers: [
{ color: '#efe9e4' },
{ lightness: -25 }
]
}
];
// Constructor creates a new map - only center and zoom are required.
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 31.200092, lng: 29.918739},
zoom: 13,
styles: styles,
mapTypeControl: false
});
// These are the real estate listings that will be shown to the user.
// Normally we'd have these in a database instead.
// locations
var locations = [
{
title: 'Alexandria Stadium',
location: {
lat: 31.197275,
lng: 29.91369
}},
{
title: 'Alexandria National Museum',
location: {
lat: 31.200955,
lng: 29.91325
}},
{
title: 'Stanley Bridge',
location: {
lat: 31.2349534,
lng: 29.9484707
}},
{
title: 'Bibliotheca_Alexandrina',
location: {
lat: 31.208872,
lng: 29.9092
}},
{
title: 'Citadel of Qaitbay',
location: {
lat: 31.2140,
lng: 29.8856
}}
];
var largeInfowindow = new google.maps.InfoWindow();
// Style the markers a bit. This will be our listing marker icon.
var defaultIcon = makeMarkerIcon('34A853');
// Create a "highlighted location" marker color for when the user
// mouses over the marker.
var highlightedIcon = makeMarkerIcon('FBBC05');
// The following group uses the location array to create an array of markers on initialize.
for (var i = 0; i < locations.length; i++) {
// Get the position from the location array.
var position = locations[i].location;
var title = locations[i].title;
// Create a marker per location, and put into markers array.
var marker = new google.maps.Marker({
position: position,
title: title,
animation: google.maps.Animation.DROP,
icon: defaultIcon,
id: i,
map: map
});
// Push the marker to our array of markers.
markers.push(marker);
// Create an onclick event to open an infowindow at each marker.
marker.addListener('click', function() {
populateInfoWindow(this, largeInfowindow) + this.setAnimation(google.maps.Animation.BOUNCE);
//toggleBounce(this);
// markers[0].setAnimation(null);
});
// Two event listeners - one for mouseover, one for mouseout,
// to change the colors back and forth.
marker.addListener('mouseover', function() {
this.setIcon(highlightedIcon);
});
marker.addListener('mouseout', function() {
this.setIcon(defaultIcon) + this.setAnimation(null);
//toglBounce(this);
});
//click on search
this.bounce = function(place) {
for (var j = 0 ; j <= locations.length; j++ ){
if ( place.Name === markers[j].title){
google.maps.event.trigger(markers[j], 'click');
break;
}
}
setTimeout((function() {
markers[j].setAnimation(null);
}), 750);
};
}
// put animation
function toggleBounce(marker) {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
// remove animation
function toglBounce(marker){
marker.setAnimation(null);
}
//search
var viewModel = {
items : [{ Name: ""},{ Name: ""}, { Name: ""}, { Name: ""}, { Name: ""}]
};
for (i = 0; i < markers.length; i++) {
viewModel.items[i].Name = markers[i].title;
viewModel.items[i].location = locations[i].location;
}
viewModel.Query = ko.observable('');
viewModel.searchResults = ko.computed(function() {
var q = viewModel.Query();
array = viewModel.items.filter(function(i){
var name = i.Name.toLowerCase().indexOf(q) >=0;
return name ;
});
//filtered markers
for ( var i = 0 ; i < markers.length ; i++){
if (array.length === 0 ){
for( var k = 0 ; k < markers.length ;k++){
markers[k].setVisible(false);
}
}
else if (array.length == markers.length){
markers[i].setVisible(true);
}
else if (array.length < markers.length){
for ( var x = 0 ; x < markers.length ; x++){
for (var j=0 ; j < array.length ; j++){
if (markers[x].title == array[j].Name){
markers[x].setVisible(true);
break;
}
else {
markers[x].setVisible(false);
}
}
}
}
else markers[i].setVisible(false);
}
return viewModel.items.filter(function(i) {
var name = i.Name.toLowerCase().indexOf(q) >= 0;
return name;
});
});
//end search
ko.applyBindings(viewModel);
}
// This function populates the infowindow when the marker is clicked. We'll only allow
// one infowindow which will open at the marker that is clicked, and populate based
// on that markers position.
function populateInfoWindow(marker, infowindow) {
//get info
var search = marker.title;
var extract;
//var getData = function(country,type){
var url= 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles='+search;
$.ajax({
url: url,
crossOrigin: true,
type: 'GET',
dataType: "jsonp",
xhrFields: { withCredentials: true },
accept: 'application/json'
}).done(function (data) {
var g = data.query.pages;
extract = g[Object.keys(g)[0]].extract;
if (infowindow.marker != marker) {
infowindow.marker = marker;
infowindow.setContent('<div>' + marker.title +'</br>'+extract+ '</div>');
infowindow.open(map, marker);
// Make sure the marker property is cleared if the infowindow is closed.
infowindow.addListener('closeclick', function() {
infowindow.marker = null;
});
}
console.log(extract);
}).fail(function (xhr, textStatus, error) {
var title, message;
switch (xhr.status) {
case 403:
title = xhr.responseJSON.errorSummary;
message = 'Please login to your server before running the test.';
document.getElementById('#error').innerHTML=message;
break;
default:
title = 'Invalid URL or Cross-Origin Request Blocked';
message = 'You must explictly add this site (' + window.location.origin + ') to the list of allowed websites in your server.';
document.getElementById('#error').innerHTML=message;
break;
}
});
var interval = setInterval(function () {
if(extract===undefined){
console.log("err");
}else{
console.log(extract);
clearInterval(interval);
}
}, 100);
//end
// Check to make sure the infowindow is not already opened on this marker.
for (var i=0;i<markers.length;i++) {
console.log(marker.title);
}
}
// handle map error
function googleMapsError() {
alert('An error occurred while loading Google Maps, Please refresh the page!');
}
// This function takes in a COLOR, and then creates a new marker
// icon of that color. The icon will be 21 px wide by 34 high, have an origin
// of 0, 0 and be anchored at 10, 34).
function makeMarkerIcon(markerColor) {
var markerImage = new google.maps.MarkerImage(
'http://chart.googleapis.com/chart?chst=d_map_spin&chld=1.15|0|'+ markerColor +
'|40|_|%E2%80%A2',
new google.maps.Size(21, 34),
new google.maps.Point(0, 0),
new google.maps.Point(10, 34),
new google.maps.Size(21,34));
return markerImage;
}
function myFunction() {
var x = document.getElementById("options");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}