Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from flask import Flask, render_template_string, jsonify, request
|
| 2 |
import sqlite3
|
| 3 |
-
import math
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
@@ -8,16 +7,14 @@ app = Flask(__name__)
|
|
| 8 |
def init_db():
|
| 9 |
conn = sqlite3.connect('markers.db')
|
| 10 |
cursor = conn.cursor()
|
| 11 |
-
cursor.execute('''
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
''')
|
| 21 |
conn.commit()
|
| 22 |
conn.close()
|
| 23 |
|
|
@@ -49,37 +46,6 @@ def remove_marker_from_db(marker_id):
|
|
| 49 |
conn.commit()
|
| 50 |
conn.close()
|
| 51 |
|
| 52 |
-
# Функция для поиска маркеров по названию или описанию в радиусе
|
| 53 |
-
def search_markers_within_radius(user_lat, user_lon, radius_km, search_term):
|
| 54 |
-
conn = sqlite3.connect('markers.db')
|
| 55 |
-
cursor = conn.cursor()
|
| 56 |
-
|
| 57 |
-
# SQL-запрос для поиска маркеров с условием расстояния
|
| 58 |
-
query = """
|
| 59 |
-
SELECT id, name, description, telegram_link, latitude, longitude,
|
| 60 |
-
(6371 * acos(cos(radians(?)) * cos(radians(latitude)) * cos(radians(longitude) - radians(?)) + sin(radians(?)) * sin(radians(latitude)))) AS distance
|
| 61 |
-
FROM markers
|
| 62 |
-
WHERE (LOWER(name) LIKE ? OR LOWER(description) LIKE ?)
|
| 63 |
-
HAVING distance <= ?
|
| 64 |
-
ORDER BY distance ASC
|
| 65 |
-
"""
|
| 66 |
-
search_term_lower = f'%{search_term.lower()}%'
|
| 67 |
-
cursor.execute(query, (user_lat, user_lon, user_lat, search_term_lower, search_term_lower, radius_km))
|
| 68 |
-
results = cursor.fetchall()
|
| 69 |
-
|
| 70 |
-
markers = [{
|
| 71 |
-
'id': row[0],
|
| 72 |
-
'name': row[1],
|
| 73 |
-
'description': row[2],
|
| 74 |
-
'telegram_link': row[3],
|
| 75 |
-
'latitude': row[4],
|
| 76 |
-
'longitude': row[5],
|
| 77 |
-
'distance': row[6]
|
| 78 |
-
} for row in results]
|
| 79 |
-
|
| 80 |
-
conn.close()
|
| 81 |
-
return markers
|
| 82 |
-
|
| 83 |
# HTML-шаблон с улучшенным дизайном
|
| 84 |
html_template = '''
|
| 85 |
<!DOCTYPE html>
|
|
@@ -150,19 +116,14 @@ html_template = '''
|
|
| 150 |
.popup-form button:hover {
|
| 151 |
background-color: #0056b3;
|
| 152 |
}
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
justify-content: center;
|
| 156 |
-
margin: 20px 0;
|
| 157 |
-
}
|
| 158 |
-
.search-container input {
|
| 159 |
padding: 8px;
|
|
|
|
| 160 |
border: 1px solid #ccc;
|
| 161 |
border-radius: 5px;
|
| 162 |
-
width: 300px;
|
| 163 |
-
margin-right: 10px;
|
| 164 |
}
|
| 165 |
-
|
| 166 |
padding: 8px 12px;
|
| 167 |
background-color: #007BFF;
|
| 168 |
color: white;
|
|
@@ -170,30 +131,26 @@ html_template = '''
|
|
| 170 |
border-radius: 5px;
|
| 171 |
cursor: pointer;
|
| 172 |
}
|
| 173 |
-
|
| 174 |
background-color: #0056b3;
|
| 175 |
}
|
| 176 |
-
#results {
|
| 177 |
margin: 20px;
|
| 178 |
-
padding: 10px;
|
| 179 |
-
background: #fff;
|
| 180 |
-
border-radius: 5px;
|
| 181 |
-
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
|
| 182 |
}
|
| 183 |
</style>
|
| 184 |
</head>
|
| 185 |
<body>
|
| 186 |
<h1>GeoGram</h1>
|
| 187 |
-
<div
|
| 188 |
-
<input type="text" id="
|
| 189 |
-
<button
|
| 190 |
</div>
|
| 191 |
-
<div id="results"></div>
|
| 192 |
<div id="map"></div>
|
| 193 |
-
|
| 194 |
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
| 195 |
<script>
|
| 196 |
-
const map = L.map('map').setView([55.75, 37.61], 13);
|
| 197 |
|
| 198 |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
| 199 |
maxZoom: 19,
|
|
@@ -249,57 +206,88 @@ html_template = '''
|
|
| 249 |
|
| 250 |
// Событие клика по карте для добавления формы
|
| 251 |
map.on('click', function(e) {
|
| 252 |
-
const
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
});
|
| 260 |
|
| 261 |
// Удаление метки
|
| 262 |
-
function removeMarker(
|
| 263 |
-
fetch(
|
| 264 |
-
method: '
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
},
|
| 268 |
-
body: JSON.stringify({ id: markerId })
|
| 269 |
-
})
|
| 270 |
-
.then(response => {
|
| 271 |
-
if (response.ok) {
|
| 272 |
-
alert('Метка удалена');
|
| 273 |
-
location.reload(); // Перезагрузить страницу для обновления меток
|
| 274 |
-
}
|
| 275 |
});
|
| 276 |
}
|
| 277 |
|
| 278 |
-
//
|
| 279 |
-
function searchMarkers() {
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
fetch(`/search_markers?term=${encodeURIComponent(searchTerm)}`)
|
| 283 |
.then(response => response.json())
|
| 284 |
.then(data => {
|
| 285 |
-
const resultsDiv = document.getElementById('results');
|
| 286 |
-
resultsDiv.innerHTML = '';
|
| 287 |
-
if (data.length
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
}
|
|
|
|
|
|
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
});
|
| 304 |
}
|
| 305 |
</script>
|
|
@@ -311,7 +299,7 @@ html_template = '''
|
|
| 311 |
def index():
|
| 312 |
return render_template_string(html_template)
|
| 313 |
|
| 314 |
-
@app.route('/get_markers')
|
| 315 |
def get_markers():
|
| 316 |
markers = get_all_markers()
|
| 317 |
return jsonify([{
|
|
@@ -340,21 +328,30 @@ def add_marker():
|
|
| 340 |
'longitude': longitude
|
| 341 |
})
|
| 342 |
|
| 343 |
-
@app.route('/remove_marker', methods=['
|
| 344 |
-
def remove_marker():
|
| 345 |
-
data = request.json
|
| 346 |
-
marker_id = data['id']
|
| 347 |
remove_marker_from_db(marker_id)
|
| 348 |
-
return
|
| 349 |
|
| 350 |
-
@app.route('/search_markers')
|
| 351 |
def search_markers():
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
if __name__ == '__main__':
|
| 359 |
init_db()
|
| 360 |
-
app.run(debug=True
|
|
|
|
| 1 |
from flask import Flask, render_template_string, jsonify, request
|
| 2 |
import sqlite3
|
|
|
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
|
|
|
| 7 |
def init_db():
|
| 8 |
conn = sqlite3.connect('markers.db')
|
| 9 |
cursor = conn.cursor()
|
| 10 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS markers (
|
| 11 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 12 |
+
name TEXT NOT NULL,
|
| 13 |
+
description TEXT,
|
| 14 |
+
telegram_link TEXT,
|
| 15 |
+
latitude REAL NOT NULL,
|
| 16 |
+
longitude REAL NOT NULL
|
| 17 |
+
)''')
|
|
|
|
|
|
|
| 18 |
conn.commit()
|
| 19 |
conn.close()
|
| 20 |
|
|
|
|
| 46 |
conn.commit()
|
| 47 |
conn.close()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# HTML-шаблон с улучшенным дизайном
|
| 50 |
html_template = '''
|
| 51 |
<!DOCTYPE html>
|
|
|
|
| 116 |
.popup-form button:hover {
|
| 117 |
background-color: #0056b3;
|
| 118 |
}
|
| 119 |
+
#search-input {
|
| 120 |
+
margin: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
padding: 8px;
|
| 122 |
+
width: calc(100% - 40px);
|
| 123 |
border: 1px solid #ccc;
|
| 124 |
border-radius: 5px;
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
+
#search-button {
|
| 127 |
padding: 8px 12px;
|
| 128 |
background-color: #007BFF;
|
| 129 |
color: white;
|
|
|
|
| 131 |
border-radius: 5px;
|
| 132 |
cursor: pointer;
|
| 133 |
}
|
| 134 |
+
#search-button:hover {
|
| 135 |
background-color: #0056b3;
|
| 136 |
}
|
| 137 |
+
#search-results {
|
| 138 |
margin: 20px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
</style>
|
| 141 |
</head>
|
| 142 |
<body>
|
| 143 |
<h1>GeoGram</h1>
|
| 144 |
+
<div>
|
| 145 |
+
<input type="text" id="search-input" placeholder="Поиск по названию или описанию" />
|
| 146 |
+
<button id="search-button">Поиск</button>
|
| 147 |
</div>
|
| 148 |
+
<div id="search-results"></div>
|
| 149 |
<div id="map"></div>
|
| 150 |
+
|
| 151 |
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
| 152 |
<script>
|
| 153 |
+
const map = L.map('map').setView([55.75, 37.61], 13); // Начальное местоположение по умолчанию
|
| 154 |
|
| 155 |
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
| 156 |
maxZoom: 19,
|
|
|
|
| 206 |
|
| 207 |
// Событие клика по карте для добавления формы
|
| 208 |
map.on('click', function(e) {
|
| 209 |
+
const popupContent = document.createElement('div');
|
| 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>
|
|
|
|
| 299 |
def index():
|
| 300 |
return render_template_string(html_template)
|
| 301 |
|
| 302 |
+
@app.route('/get_markers', methods=['GET'])
|
| 303 |
def get_markers():
|
| 304 |
markers = get_all_markers()
|
| 305 |
return jsonify([{
|
|
|
|
| 328 |
'longitude': longitude
|
| 329 |
})
|
| 330 |
|
| 331 |
+
@app.route('/remove_marker/<int:marker_id>', methods=['DELETE'])
|
| 332 |
+
def remove_marker(marker_id):
|
|
|
|
|
|
|
| 333 |
remove_marker_from_db(marker_id)
|
| 334 |
+
return '', 204
|
| 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()
|
| 346 |
+
return jsonify([{
|
| 347 |
+
'id': marker[0],
|
| 348 |
+
'name': marker[1],
|
| 349 |
+
'description': marker[2],
|
| 350 |
+
'telegram_link': marker[3],
|
| 351 |
+
'latitude': marker[4],
|
| 352 |
+
'longitude': marker[5]
|
| 353 |
+
} for marker in markers])
|
| 354 |
|
| 355 |
if __name__ == '__main__':
|
| 356 |
init_db()
|
| 357 |
+
app.run(debug=True)
|