Search code examples
google-mapspuggoogle-maps-static-api

Interpolated coordinates prevent static google map from loading


On a web page I can display a static map using something like.

http://maps.googleapis.com/maps/api/
staticmap?center=51.455041,-0.9690884&zoom=17&size=600x300
&sensor=false&markers=51.455041,-0.9690884&scale=2")

However when I try

http://maps.googleapis.com/maps/api/staticmap?
center=#{location.coords.lat},#{location.coords.lng}
&zoom=17&size=400x350&sensor=false&markers=
#{location.coords.lat},#{location.coords.lng}&scale=2"

Where #{location.coords.lat}, and #{location.coords.lng} are interpolated strings from a controller file, from this line.

coords: {lat: 51.455041, lng: -0.9690884},

All i see is a broken image.

I know that I can access lat, and lng, because when I create a paragraph

p #{location.coords.lat}

it displays as "51.455041" when the page is loaded.


Solution

  • The following is from pug's doc:

    Previous versions of Pug/Jade supported an interpolation syntax such as:

    a(href="/#{url}") Link This syntax is no longer supported.
    

    If you are using your link as img source, the correct syntax is:

    img(src='http://maps.googleapis.com/maps/api/staticmap?center=' + location.coords.lat + ',' + location.coords.lng + '&zoom=17&size=400x350&sensor=false&markers=' + location.coords.lat + ',' + location.coords.lng + '&scale=2')