From c6a5bbac80d6b6cff28d536c1f44c08f506d3fb3 Mon Sep 17 00:00:00 2001 From: liangliang Date: Sun, 25 Feb 2018 22:36:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=9C=B0=E5=9B=BE=E5=81=8F?= =?UTF-8?q?=E7=A7=BB=F0=9F=98=B1=F0=9F=98=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- owntracks/views.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/owntracks/views.py b/owntracks/views.py index 335174d..8627f7c 100644 --- a/owntracks/views.py +++ b/owntracks/views.py @@ -10,6 +10,7 @@ from django.shortcuts import render from django.http import JsonResponse from django.contrib.auth.decorators import login_required from django.views.decorators.csrf import csrf_exempt +import requests @csrf_exempt @@ -19,6 +20,7 @@ def manage_owntrack_log(request): tid = s['tid'] lat = s['lat'] lon = s['lon'] + logger.info('tid:{tid}.lat:{lat}.lon:{lon}'.format(tid=tid, lat=lat, lon=lon)) if tid and lat and lon: m = OwnTrackLog() @@ -39,6 +41,22 @@ def show_maps(request): return render(request, 'owntracks/show_maps.html') +def convert_to_amap(locations): + datas = ';'.join(map(lambda x: str(x.lon) + ',' + str(x.lat), locations)) + + key = '8440a376dfc9743d8924bf0ad141f28e' + api = 'http://restapi.amap.com/v3/assistant/coordinate/convert' + query = { + 'key': key, + 'locations': datas, + 'coordsys': 'gps' + } + rsp = requests.get(url=api, params=query) + logger.info(type(rsp.text)) + result = json.loads(rsp.text) + return result['locations'] + + @login_required def get_datas(request): models = OwnTrackLog.objects.all() @@ -49,11 +67,9 @@ def get_datas(request): d = dict() d["name"] = tid paths = list() - for i in item: - path = list() - path.append(i.lon) - path.append(i.lat) - paths.append(path) + locations = convert_to_amap(item) + for i in locations.split(';'): + paths.append(i.split(',')) d["path"] = paths result.append(d) - return JsonResponse(result, safe=False) + return JsonResponse(result, safe=False)