[Python] Using Google Maps API to plot multiple points and utilize direction function

Discussão em 'Python' iniciado por Stack, Abril 8, 2025.

  1. Stack

    Stack Membro Participativo

    For starters, I have the code working to populate the driving directions, however, it only populates the first two points. There are additional coordinates in the set that I am trying to have included. I was able to get them to populate to a Google Maps URL, but one that lacked any tool usage.screenshot of the URL showing 2 points and directions

    The below snippet of code is what is used to populate the URL. I was told that I may need to utilize Java to plot the rest of the points from the data set on a map and retain the ability to use directions, however, that is not something I am familiar with.screenshot of the line of code that creates the URL I did try removing the items after api=1, but that just pulls up google maps with no plotted points.

    # Geocode addresses to get coordinates
    coordinates = []
    for address in addresses:
    geocode_result = gmaps.geocode(address)
    if geocode_result:
    lat = geocode_result[0]['geometry']['location']['lat']
    lng = geocode_result[0]['geometry']['location']['lng']
    coordinates.append((lat, lng))
    print(f"Geocoded: {address} -> ({lat}, {lng})")


    # Initialize the map, centered on the first valid coordinate
    gmap = gmplot.GoogleMapPlotter(47.54, -100.437012, 7, apikey='API Key goes here')
    lats, lons = zip(*coordinates)
    gmap.scatter(lats, lons, 'blue', marker=True)
    gmap.draw('mymap.html')


    directions_url = f"https://www.google.com/maps/dir/?api=1&origin={lats[0]},{lons[0]}&destination={lats[1]},{lons[1]}"
    print(directions_url)

    Continue reading...

Compartilhe esta Página