Skip to content

Commit c6a5bba

Browse files
committed
修正地图偏移😱😱
1 parent 5398707 commit c6a5bba

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

owntracks/views.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.http import JsonResponse
1111
from django.contrib.auth.decorators import login_required
1212
from django.views.decorators.csrf import csrf_exempt
13+
import requests
1314

1415

1516
@csrf_exempt
@@ -19,6 +20,7 @@ def manage_owntrack_log(request):
1920
tid = s['tid']
2021
lat = s['lat']
2122
lon = s['lon']
23+
2224
logger.info('tid:{tid}.lat:{lat}.lon:{lon}'.format(tid=tid, lat=lat, lon=lon))
2325
if tid and lat and lon:
2426
m = OwnTrackLog()
@@ -39,6 +41,22 @@ def show_maps(request):
3941
return render(request, 'owntracks/show_maps.html')
4042

4143

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+
4260
@login_required
4361
def get_datas(request):
4462
models = OwnTrackLog.objects.all()
@@ -49,11 +67,9 @@ def get_datas(request):
4967
d = dict()
5068
d["name"] = tid
5169
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(','))
5773
d["path"] = paths
5874
result.append(d)
59-
return JsonResponse(result, safe=False)
75+
return JsonResponse(result, safe=False)

0 commit comments

Comments
 (0)