Aleksmorshen commited on
Commit
1d6508e
·
verified ·
1 Parent(s): de03262

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -35
app.py CHANGED
@@ -210,59 +210,91 @@ html_template = '''
210
  popupContent.classList.add('popup-form');
211
 
212
  const inputName = document.createElement('input');
213
- inputName.placeholder = 'Название';
 
 
214
  const inputDescription = document.createElement('textarea');
215
- inputDescription.placeholder = 'Описание';
 
216
  const inputTelegram = document.createElement('input');
217
- inputTelegram.placeholder = 'Telegram ссылка';
218
- const submitButton = document.createElement('button');
219
- submitButton.innerText = 'Добавить метку';
220
 
221
- submitButton.onclick = function() {
222
- const name = inputName.value;
223
- const description = inputDescription.value;
224
- const telegramLink = inputTelegram.value;
225
- addMarker(name, description, telegramLink, e.latlng);
226
- map.closePopup();
 
 
 
 
227
  };
228
 
229
  popupContent.appendChild(inputName);
230
  popupContent.appendChild(inputDescription);
231
  popupContent.appendChild(inputTelegram);
232
- popupContent.appendChild(submitButton);
233
- L.popup().setLatLng(e.latlng).setContent(popupContent).openOn(map);
 
 
 
 
234
  });
235
 
236
- // Функция для удаления метки
237
- function removeMarker(markerId) {
238
- fetch(`/remove_marker/${markerId}`, { method: 'DELETE' })
239
- .then(() => {
240
- location.reload(); // Перезагрузка страницы для обновления меток
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  });
242
  }
243
 
244
- // Обработка поиска меток
245
- document.getElementById('search-button').addEventListener('click', function() {
246
  const query = document.getElementById('search-input').value;
247
- fetch(`/search_markers?query=${query}`)
 
 
 
 
 
248
  .then(response => response.json())
249
  .then(data => {
250
- const searchResultsDiv = document.getElementById('search-results');
251
- searchResultsDiv.innerHTML = ''; // Очистка предыдущих результатов
252
-
253
- data.forEach(marker => {
254
- const resultItem = document.createElement('div');
255
- resultItem.innerHTML = `<b>${marker.name}</b> - ${marker.description} <a href="https://t.me/${marker.telegram_link}" target="_blank">Перейти в Telegram</a>`;
256
- searchResultsDiv.appendChild(resultItem);
257
- });
258
  });
259
- });
260
  </script>
261
  </body>
262
  </html>
263
  '''
264
 
265
- # Определение маршрутов
266
  @app.route('/')
267
  def index():
268
  return render_template_string(html_template)
@@ -281,12 +313,11 @@ def get_markers():
281
 
282
  @app.route('/add_marker', methods=['POST'])
283
  def add_marker():
284
- data = request.get_json()
285
  name = data['name']
286
  description = data['description']
287
  telegram_link = data['telegram_link']
288
  latitude, longitude = data['position']
289
-
290
  marker_id = add_marker_to_db(name, description, telegram_link, latitude, longitude)
291
  return jsonify({
292
  'id': marker_id,
@@ -304,11 +335,11 @@ def remove_marker(marker_id):
304
 
305
  @app.route('/search_markers', methods=['GET'])
306
  def search_markers():
307
- query = request.args.get('query', '').lower() # Приведение к нижнему регистру для поиска
308
  conn = sqlite3.connect('markers.db')
309
  cursor = conn.cursor()
310
  cursor.execute('''SELECT id, name, description, telegram_link, latitude, longitude FROM markers
311
- WHERE LOWER(name) LIKE ? OR LOWER(description) LIKE ?''',
312
  (f'%{query}%', f'%{query}%'))
313
  markers = cursor.fetchall()
314
  conn.close()
 
210
  popupContent.classList.add('popup-form');
211
 
212
  const inputName = document.createElement('input');
213
+ inputName.type = 'text';
214
+ inputName.placeholder = 'Название метки';
215
+
216
  const inputDescription = document.createElement('textarea');
217
+ inputDescription.placeholder = 'Описание метки';
218
+
219
  const inputTelegram = document.createElement('input');
220
+ inputTelegram.type = 'text';
221
+ inputTelegram.placeholder = 'Имя пользователя Telegram (без https://t.me/)';
 
222
 
223
+ const button = document.createElement('button');
224
+ button.textContent = 'Добавить метку';
225
+ button.onclick = function() {
226
+ const markerName = inputName.value;
227
+ const markerDescription = inputDescription.value;
228
+ const markerTelegram = inputTelegram.value;
229
+ if (markerName && markerTelegram) {
230
+ addMarker(markerName, markerDescription, markerTelegram, e.latlng);
231
+ map.closePopup();
232
+ }
233
  };
234
 
235
  popupContent.appendChild(inputName);
236
  popupContent.appendChild(inputDescription);
237
  popupContent.appendChild(inputTelegram);
238
+ popupContent.appendChild(button);
239
+
240
+ const popup = L.popup()
241
+ .setLatLng(e.latlng)
242
+ .setContent(popupContent)
243
+ .openOn(map);
244
  });
245
 
246
+ // Удаление метки
247
+ function removeMarker(id) {
248
+ fetch(`/remove_marker/${id}`, {
249
+ method: 'DELETE'
250
+ }).then(() => {
251
+ location.reload();
252
+ });
253
+ }
254
+
255
+ // Функция для поиска меток
256
+ function searchMarkers(query) {
257
+ fetch(`/search_markers?query=${encodeURIComponent(query)}`)
258
+ .then(response => response.json())
259
+ .then(data => {
260
+ const resultsDiv = document.getElementById('search-results');
261
+ resultsDiv.innerHTML = ''; // Очистить предыдущие результаты
262
+ if (data.length > 0) {
263
+ data.forEach(marker => {
264
+ const resultItem = document.createElement('div');
265
+ resultItem.innerHTML = `<b>${marker.name}</b>: ${marker.description} <button onclick="showMarker(${marker.id})">Показать на карте</button>`;
266
+ resultsDiv.appendChild(resultItem);
267
+ });
268
+ } else {
269
+ resultsDiv.innerHTML = '<p>Нет результатов</p>';
270
+ }
271
  });
272
  }
273
 
274
+ // Обработчик события для кнопки поиска
275
+ document.getElementById('search-button').onclick = function() {
276
  const query = document.getElementById('search-input').value;
277
+ searchMarkers(query);
278
+ };
279
+
280
+ // Функция для показа метки на карте
281
+ function showMarker(markerId) {
282
+ fetch(`/get_markers`)
283
  .then(response => response.json())
284
  .then(data => {
285
+ const marker = data.find(m => m.id === markerId);
286
+ if (marker) {
287
+ map.setView([marker.latitude, marker.longitude], 13);
288
+ L.marker([marker.latitude, marker.longitude]).addTo(map)
289
+ .bindPopup(`<b>${marker.name}</b><p>${marker.description}</p><a href="https://t.me/${marker.telegram_link}" target="_blank">Перейти в Telegram</a>`);
290
+ }
 
 
291
  });
292
+ }
293
  </script>
294
  </body>
295
  </html>
296
  '''
297
 
 
298
  @app.route('/')
299
  def index():
300
  return render_template_string(html_template)
 
313
 
314
  @app.route('/add_marker', methods=['POST'])
315
  def add_marker():
316
+ data = request.json
317
  name = data['name']
318
  description = data['description']
319
  telegram_link = data['telegram_link']
320
  latitude, longitude = data['position']
 
321
  marker_id = add_marker_to_db(name, description, telegram_link, latitude, longitude)
322
  return jsonify({
323
  'id': marker_id,
 
335
 
336
  @app.route('/search_markers', methods=['GET'])
337
  def search_markers():
338
+ query = request.args.get('query', '')
339
  conn = sqlite3.connect('markers.db')
340
  cursor = conn.cursor()
341
  cursor.execute('''SELECT id, name, description, telegram_link, latitude, longitude FROM markers
342
+ WHERE name LIKE ? OR description LIKE ?''',
343
  (f'%{query}%', f'%{query}%'))
344
  markers = cursor.fetchall()
345
  conn.close()