Search code examples
pythongoogle-mapsgoogle-maps-static-api

Google Static Maps API: Generating a static map with paths


I'm trying to generate a static google map with some points on it, and some lines connecting these points (I'm soon going to make the lines correspond with the driving directions, but that comes later). Right now I've got code like this to generate the URL:

def getStaticMapAddress(self, route):
    url = "http://maps.google.com/maps/api/staticmap?center="+str(route[0].location.lat)+","+str(route[0].location.lng)+"&zoom=6&size=400x400&markers="
    i=0
    while i<len(route):
        url += str(route[i].location.lat)+","+str(route[i].location.lng)
        i=i+1
        if (i < len(route)):
            url += "|"
    url += "&path=color:0xff0000ff&weight:5"
    i=0
    while i<len(route):
        url += "|"+str(route[i].location.lat)+","+str(route[i].location.lng)
        i+=1
    url += "&sensor=false"
    return url

In this function, the 'route' is a list of users with associated locations. With my test data, this URL was generated:

http://maps.google.com/maps/api/staticmap?center=50.8202008,-0.1324898&zoom=6&size=400x400&markers=50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&path=color:0xff0000ff&weight:5|50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&sensor=false

If you look at that static map, you can see the markers but not the paths. I've been looking at the documentation for this (http://code.google.com/apis/maps/documentation/staticmaps/#Paths) and I can't see where I've gone wrong. Looking at the examples my URL seems to have the exact same format as the examples. Does anyone know what I'm doing wrong?

Thanks

Ben


Solution

  • There is something wrong with the color parameter of the paths. The following URI works for me:

    http://maps.google.com/maps/api/staticmap?center=50.8202008,-0.1324898&zoom=6&size=400x400&markers=50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&path=weight:5|50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&sensor=false

    and I see the default blue lines.

    Update: the "something wrong" is that the separator between the path color and path weight is a pipe symbol, not an ampersand. This:

    http://maps.google.com/maps/api/staticmap?center=50.8202008,-0.1324898&zoom=6&size=400x400&markers=50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&path=color:0xff0000ff|weight:5|50.8202008,-0.1324898|51.447341,-0.0761212|51.4608947,-2.5884312&sensor=false

    works and gives a red line.