Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 424
benwad Avatar asked Feb 22 '11 20:02

benwad


People also ask

Is Google maps static API free?

The Maps Static API uses a pay-as-you-go pricing model. Requests for the Maps Static API are billed under the SKU for Static Maps. Along with the overall Google Terms of Use, there are usage limits specific to the Maps Static API.

Does Google Static maps API requires a maps API key?

You could serve up to 100,000 static map views or 28,000 static Street View loads within the monthly $200 billing credit that gets applied to your Google Maps Platform usage. However, using the static imagery APIs requires you to include your API key in publicly viewable code.

Is Google Maps dynamic or static?

Since the map produced is a static image, it won't be interactive or tailored to the user but it can be stylised through the custom parameters.


1 Answers

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.

like image 158
Andrew Walker Avatar answered Sep 28 '22 06:09

Andrew Walker