2017-07-05 45 views
0

geopyとGoogle APIを使用しようとしています。私は、モデルを介して住所を取得し、geopyのロケータにフィードし、Google Maps APIの緯度/経度を知らせる住所詳細を取得しています。 a。)ユニークなlat/lng座標のセットを取得する b)テンプレート をプルダウンします。c)リストをループし、複数のピンをマップに配置します。Django - geopyを使用してGoogleマップテンプレートを設定する

ここに私が現在持っているものがあります。テンプレートにレンダリングするためのコンテキストでループを取得する方法が不明です。

def maps_multi(request): 

address_list = CustomerOrder.objects.filter(city__icontains = 'CA').distinct() # pulls all unique addresses in California 
address_list1 = address_list[0:2] # temporarily select first three addresses from list to test 

geolocator = GoogleV3() 


for x in address_list1: 
    add_list = [] 
    add_city = x.city 
    location = geolocator.geocode(add_city) 
    add_list.append(location) 
    print(add_list) 

context = {} 

return render(request, 'orderdisplay/maps_multi.html', context) 

答えて

0

私はちょうどこの同じことを経験し、まだもっと学びます。 私のリストがあったら、それをテンプレートに送って地図上にポイントを表示するために繰り返しました。店舗

return render(request, 'myapp/sometemplate.html', {'form': form, 'stores': stores}) 

その後、私のテンプレートで、私はそのリストを反復処理する

:この場合、私は、「店舗」を可決しました。必要に応じて、それらの点をマップ上にプロットすることができます。

   {% for store in stores %} 
       latlng = new google.maps.LatLng("{{ store.location.y }}", "{{ store.location.x }}"); 
       new google.maps.Marker({ 
        position: latlng, 
        map: map, 
        title: "{{ store.address }}" 
       }); 
      {% endfor %} 
関連する問題