I want to display the nearest services points using the distance function on the Folium map. I need to pass the user location from the form and find the nearest services based on the current location. This is what I have my views.py file:

from django.shortcuts import render, get_object_or_404
from .forms import addressModelForm
from django.views import generic
from django.views.generic import TemplateView
from django.views.generic.list import ListView
from django.core.serializers import serialize
from django.http import HttpResponse
from django.contrib.gis.geos import Point
from django.contrib.gis.db.models.functions import Distance
from .models import Services, GautengPlaces, Servicesgp, hospitals, Pointdistance
from geopy.geocoders import Nominatim
from django.contrib.gis import geos, measure
from django.contrib.gis.db import models
import folium
from urllib import request

# Parktown long/lat
latitude = -26.167
longitude = 28.033

# Turffontein long/lat
#latitude = -26.2446
#longitude = 28.0397

user_location_init = Point(latitude, longitude, srid=4326)

# Create your views here.

def calculate_distance_view(request):
    form = addressModelForm(request.POST or None)
    glocator = Nominatim(user_agent='reporter')
    model = Services
    context_object_name = "services"
    m = folium.Map(width=800, height=500, location=user_location_init, zoom_start=10)

    if form.is_valid():
         user_address = form.cleaned_data.get('user_location')
         user_location = glocator.geocode(user_address)
         d_lat = user_location.latitude
         d_long = user_location.longitude
         current_location = Point(d_lat, d_long, srid=4326)
         nearbyobj = Services.objects.annotate(distance=Distance("location", current_location)).order_by("distance")[0:3]

         m = folium.Map(width=800, height=500, location=user_location_init, zoom_start=10)
         folium.Marker([d_lat, d_long], tooltip='click here for more', popup=user_location,
                       icon=folium.Icon(color='red', icon='cloud')).add_to(m)

    m = m._repr_html_()

    context = {
        'form': form,
        'model': model,
        'nearbyobj': 'nearbyobj',
        'map': m
    return render(request, 'index.html', context)

Models.py to pull the services from the model

class Services(models.Model):
    name = models.CharField(max_length=254)
    location = models.PointField(srid=4326)

    def __unicode__(self):
            return self.name

    class Meta:
        verbose_name_plural = " Services"

I'm not sure how to go about using

nearbyobj = Services.objects.annotate(distance=Distance("location", current_location)).order_by("distance")[0:3]

to display the nearest services based on the value we get from the form(user_location). I'm fairly new to GeoDjango, trying to get this project up and running.

1 Answer 1


You seem to be trying to display the 4 nearest locations in the Services class. The line

nearbyobj = Services.objects.annotate(distance=Distance("location", current_location)).order_by("distance")[0:3]

should work fine, given you have the Services class properly defined with the location field properly set up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.