10
10
from django .http import JsonResponse
11
11
from django .contrib .auth .decorators import login_required
12
12
from django .views .decorators .csrf import csrf_exempt
13
+ import requests
13
14
14
15
15
16
@csrf_exempt
@@ -19,6 +20,7 @@ def manage_owntrack_log(request):
19
20
tid = s ['tid' ]
20
21
lat = s ['lat' ]
21
22
lon = s ['lon' ]
23
+
22
24
logger .info ('tid:{tid}.lat:{lat}.lon:{lon}' .format (tid = tid , lat = lat , lon = lon ))
23
25
if tid and lat and lon :
24
26
m = OwnTrackLog ()
@@ -39,6 +41,22 @@ def show_maps(request):
39
41
return render (request , 'owntracks/show_maps.html' )
40
42
41
43
44
+ def convert_to_amap (locations ):
45
+ datas = ';' .join (map (lambda x : str (x .lon ) + ',' + str (x .lat ), locations ))
46
+
47
+ key = '8440a376dfc9743d8924bf0ad141f28e'
48
+ api = 'http://restapi.amap.com/v3/assistant/coordinate/convert'
49
+ query = {
50
+ 'key' : key ,
51
+ 'locations' : datas ,
52
+ 'coordsys' : 'gps'
53
+ }
54
+ rsp = requests .get (url = api , params = query )
55
+ logger .info (type (rsp .text ))
56
+ result = json .loads (rsp .text )
57
+ return result ['locations' ]
58
+
59
+
42
60
@login_required
43
61
def get_datas (request ):
44
62
models = OwnTrackLog .objects .all ()
@@ -49,11 +67,9 @@ def get_datas(request):
49
67
d = dict ()
50
68
d ["name" ] = tid
51
69
paths = list ()
52
- for i in item :
53
- path = list ()
54
- path .append (i .lon )
55
- path .append (i .lat )
56
- paths .append (path )
70
+ locations = convert_to_amap (item )
71
+ for i in locations .split (';' ):
72
+ paths .append (i .split (',' ))
57
73
d ["path" ] = paths
58
74
result .append (d )
59
- return JsonResponse (result , safe = False )
75
+ return JsonResponse (result , safe = False )
0 commit comments