-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
46 lines (32 loc) · 1.21 KB
/
main.py
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
from flask import Flask, request, jsonify, Response
import os
import json
from geopy.geocoders import Nominatim
geo_local = Nominatim(user_agent='South Korea', timeout=None)
def get_address_from_coordinate(lat, lng):
try:
address = geo_local.reverse([lat, lng], exactly_one=True, language='ko')
detail_address = address.address # 상세주소
address_parts = detail_address.split(',')
area1 = address_parts[-3].strip()
area2 = address_parts[-4].strip()
area3 = address_parts[-5].strip()
full_address = f"{area1} {area2} {area3}"
return {
"full_address": full_address,
"area1": area1,
"area2": area2,
"area3": area3
}
except:
return [0, 0]
app = Flask(__name__)
@app.route('/find_district', methods=['GET'])
def find_district_api():
lon = float(request.args.get('lon'))
lat = float(request.args.get('lat'))
address = get_address_from_coordinate(lat, lon)
response_json = json.dumps(address, ensure_ascii=False)
return Response(response=response_json, status=200, mimetype='application/json')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=3030)