-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
191 lines (167 loc) · 5.11 KB
/
Copy pathmain.js
File metadata and controls
191 lines (167 loc) · 5.11 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
var map;
document.querySelector('input#radius').value = 500;
var selected;
var locator = {
ready : false,
position : ''
}
var reselect_btn = document.querySelector('#randomise');
var position = function() {
var current_position;
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(locatorCheck, errorMessage,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 });
}
else {
alert("Geolocation is not supported by this browser");
}
function locatorCheck( location ) {
if ( typeof location.coords === "object" ) {
locator.ready = true;
locator.position = location;
}
console.log(locator.ready);
console.log(locator.position);
mapInit(locator.position);
}
}
function errorMessage(message) {
alert ( 'Geolocation failed' );
}
function mapInit ( position ) {
var London = { lat: 51.5287718, lng : -0.2416814 };
map = new google.maps.Map(document.getElementById('map'), {
center: London,
zoom: 15
});
setCurrentPos ( position );
}
function setCurrentPos( position ) {
var geoCoords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
map.setCenter( geoCoords );
initSelection( geoCoords );
}
function initSelection( position ) {
var x = radius;
findLocal( position , radius );
document.querySelector('input#radius').value = x;
console.log('init' + position);
}
function findLocal( target_pos , radius ) {
var request = {
location: target_pos,
radius: document.querySelector('input#radius').value,
types: ['cafe', 'food', 'meal_takeaway', 'meal_delivery']
};
service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK)
console.log(results);
randomiseChoices(results);
}
}
function randomiseChoices(results) {
var rng = Math.floor(Math.random() * (results.length)) + 1;
var i = 0;
results.sort(function() { return 0.5 - Math.random() });
var interval = window.setInterval( selectItem, 100 );
function selectItem() {
var last = i === results.length - 1;
if ( last ) {
clearInterval( interval );
} else {
i++;
if ( i === rng ) {
var chosen = results[i];
results.splice( i, 1 );
results.push( chosen );
}
}
selected = ( last );
var place = results[i];
createMarker(place);
}
}
var placeLoc;
function createMarker(place) {
var alpha = (selected) ? 1 : 0.2;
placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: placeLoc,
opacity: alpha
});
infoWindow(place, marker);
google.maps.event.addListener(marker, 'click', function() {
var marker_map = this.getMap();
this.info.open(marker_map, marker);
});
}
function infoWindow(place, marker) {
marker.info = new google.maps.InfoWindow({
content: buildContent(place),
maxWidth: 500
});
if (selected) {
var marker_map = marker.getMap();
marker.info.open(marker_map, marker);
var lat_adjust = placeLoc.lat() + 0.003;
map.setCenter({ lat: lat_adjust, lng: placeLoc.lng() });
}
}
function buildContent(place) {
var html_content = {};
var content = {
name : place.name,
img : getPlacePhoto(place),
address : place.vicinity,
rating : place.rating
}
for( var key in content ) {
if(content[key] === undefined) {
delete content[key];
continue;
}
switch(key) {
case "name":
html_content.name = '<h3 class="place-name">'+content['name']+'</h3>';
break;
case "img":
html_content.img = '<img src="'+content['img']+'" class="place-photo"/>';
break;
case "address":
html_content.address = '<p>'+content['address']+'</p>';
break;
case "rating":
html_content.rating = '<span class="rating">'+content['rating']+'✮</span>';
break;
default:
break;
}
}
return concatenate ( html_content );
}
function concatenate ( object ) {
var string = '';
for( key in object ) {
string = string + object[key];
}
return string;
}
function getPlacePhoto(place) {
if ( place.hasOwnProperty( 'photos ') )
return place.photos[0].getUrl({ maxWidth: 300, maxHeight: 300 });
}
reselect_btn.addEventListener('click', position, false);
/**
/* pipeline:
/*
/* show selected place info (get place by id from places library)
/* - Category
/* - Opening Hours
/* - Tel
/* - Address
/* decouple selection function from data object retrieval
*/