spiritman
spiritman

Reputation: 65

How convert miles km in geodjango

I have this view that works fine but I am finding difficult to display the result in km. I calculated the distance between two points but I get the results in meters but I want it in kilometers.

vews.py

from django.contrib.gis.db.models.functions import Distance



class SearchResultsView(generic.TemplateView):
    template_name = "search.html"

    def get_context_data(self, **kwargs):
        context = super(SearchResultsView, self).get_context_data(**kwargs)
        query = self.request.GET.get('q', default='')
        location_ = self.request.GET.get('location')
        geolocator = Nominatim(user_agent="geo", timeout=10000)
        location = geolocator.geocode(location_)
        print(location)
        new = Point(location.latitude, location.longitude, srid=4326)
        context.update({
            'job_listing': JobListing.objects.annotate(
                distance=Distance("location_on_the_map", new)
            ).filter(Q(job_title__icontains=query)).order_by("distance")
        })
        return context


Upvotes: 0

Views: 347

Answers (1)

Gavin Burnell
Gavin Burnell

Reputation: 51

According to the geodjango docs the Distance object you are annotating into your JobListing queryset will do the conversion for you - you just need to refer to distance.km or distance.mi

Upvotes: 1

Related Questions