getPlaceList.js
6.18 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
var pos;
var infowindow;
var service;
var result_entertainment = []
var result_food = []
var result_room = []
function initMap() {
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var map = new google.maps.Map(document.getElementById('map'), {
center: pos,
zoom: 17
});
infowindow = new google.maps.InfoWindow();
map.setCenter(pos);
service = new google.maps.places.PlacesService(map);
searchPlace('bar','food');
searchPlace('cafe','food');
searchPlace('meal_delivery','food');
searchPlace('meal_takeaway','food');
searchPlace('restaurant','food');
searchPlace('bakery','food');
searchPlace('department_store','entertainment');
searchPlace('movie_theater','entertainment');
searchPlace('museum','entertainment');
searchPlace('night_club','entertainment');
searchPlace('shopping_mall','entertainment');
searchPlace('zoo','entertainment');
searchPlace('lodging','room');
// put data to db
for (var i = 0; i < results_food.length; i++) {
putDataToDB(results_food[i], 'food')
}
for (var i = 0; i < results_entertainment.length; i++) {
putDataToDB(results_entertainment[i], 'entertainment')
}
for (var i = 0; i < results_room.length; i++) {
putDataToDB(results_room[i], 'room')
}
});
}
}
function searchPlace(str, placeType) {
switch(placeType) {
case 'food':
service.nearbySearch({
location: pos,
radius: 500,
type: [str]
}, callback_foods);
break;
case 'entertainment':
service.nearbySearch({
location: pos,
radius: 500,
type: [str]
}, callback_entertainment);
break;
case 'room':
service.nearbySearch({
location: pos,
radius: 500,
type: [str]
}, callback_rooms);
break;
default:
break;
}
}
function callback_foods(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
result_food.push(results[i]);
createMarker_foods(results[i]);
}
}
}
function callback_entertainment(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
result_entertainment.push(results[i]);
createMarker_entertainment(results[i]);
}
}
}
function callback_rooms(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
result_room.push(results[i]);
createMarker_rooms(results[i]);
}
}
}
function createMarker_foods(place) {
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon : "./icons/restaurant-15.svg",
//fillcolor : "#FF0000"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
function createMarker_entertainment(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon : "./icons/gaming-15.svg"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
function createMarker_rooms(place) {
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon : "./icons/lodging-15.svg"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(map, this);
});
}
function putDataToDB(result, category1) {
const id = result;
const place_id =result;
const name = result;
const address = result;
let category_big = category1
const category_small = result;
const image = result;
const rating = result;
const lng = result;
const lat =result;
const QueryCheck = () => {
if (!id || !place_id || !name || !address || !category_big || !category_small || !image || !rating || !lng || !lat) {
return Promise.reject({
message: 'Query Error'
})
}
return Promise.resolve()
}
// 2. SQL Start
const SQLStart = async (pool) => {
try {
let data = await pool.query('INSERT INTO PLACE(ID, PLACE_ID, NAME, ADDRESS, CATEGORY_BIG, CATEGORY_SMALL, IMAGE, RATING, LNG, LAT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);', [id, place_id, name, address, category_big, category_small, image, rating, lng, lat])
return Promise.resolve(pool)
} catch(err) {
return Promise.reject(err)
}
}
// 3. Response
const Response = (rows) => {
return res.status(200).json(rows)
}
// 1. Query Check
const FindQueryCheck = () => {
if (!keyword) {
return Promise.reject({
message: 'Query Error'
})
}
else return pool
}
// 2. SQL Start
const FindSQLStart = (pool) => {
return pool.query(`SELECT * FROM PLACE WHERE ID LIKE '%${id}%')
}
// 3. Response
const FindResponse = (rows) => {
return res.status(200).json(rows)
}
if([] == FindQueryCheck()
.then(FindSQLStart)
.then(FindResponse)
.catch(err => {
if (err) {
return res.status(500).json(err.message || err)
}
}))
{
QueryCheck()
.then(SQLStart)
.then(Response)
.catch(err => {
if (err) {
return res.status(500).json(err.message || err)
}
})
}
}