How can include the google maps library in my Django project?

Hello! I would need a bit of help. I’ve been trying to include the library in my project and i haven’t been able to make it work.
The library i want to integrate:

But when i call it from a View it doesn’t work.

i would appreciate any help.
Thanks.

We’ll need to see the view in which you’re trying to use this - including your current attempt at using that service, along with any associated code used by that view it order to help. (e.g. if your view is accessing a Model, it’ll help if you include the model along with the view.)

When you post code snippets here, please insert them between lines containing only 3 backticks - '. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks - ```, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely.

1 Like

Thanks for the quick answer! In the end i couldn’t make it work and i had to set my own code, i will share it in case someone needs it, thanks for the advice about the backticks!

With this code below you can get the distance and the time for a route between two given addresses.

Previously i have created an instance of the model “Pedido” which included the addresses among other data.

I also set the metric units to get the distance in Kms.

Cheers

def resumenServicioReparto(request):
    api_key = 'YOURAPIKEY'
    context ={}
    context['data'] = Pedido.objects.get(estado='ENPROCESO')
    #Extract the addresses from the context.
    direccion_de_recogida = context['data'].direccion_de_recogida
    direccion_de_entrega = context['data'].direccion_de_entrega
    #Calculate the distance and time between with google matrix
    # Url de distance matrix 
    url ='https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&'
    # Get method of requests module
    # return response object
    r = requests.get(url + 'origins=' + direccion_de_recogida + '&destinations=' + direccion_de_entrega + '&key=' + api_key)
    tiempo_de_ruta = r.json()["rows"][0]["elements"][0]["duration"]["text"]
    distancia_de_la_ruta = r.json()["rows"][0]["elements"][0]["distance"]["text"]
    context['datos_ruta'] = { 'distancia': distancia_de_la_ruta, 'tiempo': tiempo_de_ruta}
    return render(request, "backend/resumen_pedido.html", context)