0

I am trying to use geopy and the Google api. I am querying through models to pull address city,state address detail that will feed into geopy's locator and give me lat/lng for the Google Maps api. I would like to: a.) Get a unique set of lat/lng coordinates b.) Pull into template c.) Loop through list and post multiple pins on a map.

Here is what i currently have in views. Not sure how to get the loop in the context for rendering into the template.

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)

1 Answers1

0

I just went through this same thing and still learning more. Once I had my list I sent it to the template and iterated over it to show points on a map. In this case I passed 'stores':stores

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

Then in my template I iterate over that list. You can then plot those points on a map if you want.

             {% 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 %}