-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.js
211 lines (168 loc) · 5.72 KB
/
map.js
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
//Map Initialization
var mapOptions, map, infowindow;
//Search Box
var searchBox;
//Map Marker
var marker = null, myLatlng, place;
//Marker Images
var markerGreen, markerPurple;
//Overlapping Marker Spiderifier
var oms, iw;
//For query
var keywords, llat, llong, radius;
//For post-query marking
var eventData, eventMarkers;
function initialize()
{
//Create Map
mapOptions = { center: { lat: 0, lng: 0}, zoom: 2 };
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
//Create Spiderifier
oms = new OverlappingMarkerSpiderfier(map);
//OMS listener - set infowindow to open and close.
iw = new google.maps.InfoWindow({ maxWidth: 500 });
oms.addListener('click', function(marker, event) {
iw.setContent(marker.desc);
iw.open(map, marker);
});
oms.addListener('spiderfy', function(markers) {
iw.close();
});
// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */(document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
searchBox = new google.maps.places.Autocomplete( /** @type {HTMLInputElement} */(input) );
//Add event keywords input
input = /** @type {HTMLFormElement} */(document.getElementById('keyform'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
//Add footer text to map
input = /** @type {HTMLDivElement} */(document.getElementById('footer'));
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(input);
//Default Location Marker
myLatlng = new google.maps.LatLng(0.1,0.1);
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: '0,0'
});
//Attempt to change starting location to user's current location.
geolocate();
//Event for search bar - Move map to new location and set marker
google.maps.event.addListener(searchBox, 'place_changed', function()
{
place = searchBox.getPlace();
myLatlng = new google.maps.LatLng(place.geometry.location.lat(),place.geometry.location.lng());
if(map.getZoom() < 13)
map.setZoom(13);
map.setCenter(myLatlng);
marker.setPosition(myLatlng);
marker.setTitle(place.name.toString());
});
}
function query()
{
//Get search terms
var bounds = map.getBounds();
var center = bounds.getCenter();
llat = center.lat();
llong = center.lng();
radius = getMapRadius();
keywords = document.getElementById('pac-input-keyword').value;
//Send AJAX request for meetup query
var url = './meetup.php?lat='+llat+'&long='+llong+'&q='+keywords+'&r='+radius;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
eventData = eval("("+xmlhttp.responseText+")");
document.getElementById("loading").style.display = "none";
processEvents();
}
}
document.getElementById("loading").style.display = "block";
xmlhttp.open("GET",'./meetup.php?lat='+llat+'&long='+llong+'&q='+keywords+'&r='+radius,true);
xmlhttp.send();
}
//Add markers after running API query
function processEvents()
{
//If no results are returned, stop.
if(eventData == '0')
{
return 0;
}
//Delete older event markers
if(eventMarkers != null)
for(var i=0; i<eventMarkers.length; i++)
eventMarkers[i].setMap(null);
eventMarkers = new Array();
var mIcon, mSize;
//For each item in query result, initialize marker and info window
for(var i=0; i< eventData.length; i++)
{
if(eventData[i]['fee'] == 0)
mIcon = 'green';
else
mIcon = 'purple';
mSize = 32+eventData[i]['yes_rsvp_count']/2;
var image = {
url: './images/'+mIcon+'-dot.png',
scaledSize: new google.maps.Size(mSize, mSize),
};
//Create an event marker
eventMarkers[i] = new google.maps.Marker({
position: new google.maps.LatLng(eventData[i]['group_lat'], eventData[i]['group_lon']),
map: map,
icon: image,
title: eventData[i]['name'],
size: new google.maps.Size(mSize, mSize)
});
//Set description (to be used as InfoWindow text)
eventMarkers[i].desc = '<h3><a href="'+eventData[i]['event_url']+'" target="_blank">'+eventData[i]['name']+'</a></h3>'+'<i>'+eventData[i]['group_name']+
'</i><br>'+eventData[i]['yes_rsvp_count']+' attending<br>'+
'Fee: '+eventData[i]['fee']+'<br>'+new Date(eventData[i]['time']);
//Add marker to OMS
oms.addMarker(eventMarkers[i]);
}
}
//Geolocation feature
function geolocate()
{
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
myLatlng = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
if(map.getZoom() < 13)
map.setZoom(13);
marker.setPosition(myLatlng);
marker.setTitle("Your Location");
map.setCenter(myLatlng);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function getMapRadius()
{
//Source: http://stackoverflow.com/questions/3525670/radius-of-viewable-region-in-google-maps-v3
var bounds = map.getBounds();
var center = bounds.getCenter();
var ne = bounds.getNorthEast();
// r = radius of the earth in statute miles
var r = 3963.0;
// Convert lat or lng from decimal degrees into radians (divide by 57.2958)
var lat1 = center.lat() / 57.2958;
var lon1 = center.lng() / 57.2958;
var lat2 = ne.lat() / 57.2958;
var lon2 = ne.lng() / 57.2958;
// distance = circle radius from center to Northeast corner of bounds
var dis = r * Math.acos(Math.sin(lat1) * Math.sin(lat2) +
Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1));
return dis;
}
//Initialize Map
google.maps.event.addDomListener(window, 'load', initialize);