Google static map shot showing up on page
I have a site built on the Google App Engine in Python. One of the features is the ability to use google static maps to update your current location. The Python code takes care of generating the URL of the static map and the HTML template simply puts it in place. This is what the space where the image should go looks like:
<tr>
<td colspan="4"><img border=0 src="{{img_url}}" /></td>
</tr>
...and then here's how the image URL is generated:
self.template_values['img_url'] = "http://maps.google.com/staticmap?center=%s,%s&zoom=%s&size=%s&markers=%s,%s,midreda" % (str(rs['lat']), str(rs['lng']), str(rs['zoom']), str(MAP_SIZE[0])+"x"+str(MAP_SIZE[1]), str(rs['lat']), str(rs['l开发者_如何学Gong']))
The rs[] dict is the response from the google maps search. This generates a perfectly valid static map URL:
<tr>
<td colspan="4"><img border=0 src="http://maps.google.com/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=52.955115,-1.149172,midreda" /></td>
</tr>
The above image URL is after a search for 'Nottingham, UK'. If you visit the address you can see it shows up as intended, correct size and everything. However, on the page (which you can check at spare-wheels.appspot.com/searchlocation, although you need to connect your Facebook account to use it) the image doesn't show up. The HTML source looks fine: no apparent encoding problems or anything like that. I can't see what's gone wrong. Any ideas?
Thanks
Ben
EDIT: After a bit of testing I've found that the maps sometimes work but most of the time they don't, and when they work they generally stop working after a refresh
Google Maps API v1 is deprecated. The notice concerning free static map usage no longer needing an API Key is likely only valid to applications using Google Maps API V2.
Browser requests containing your referer are being denied with 403. Hence why it displays the broken image icon on your browser.
Either switch to v2 URL's, or supply an API key in the url.
Google provides an upgrade guide for your convenience.
The correct v2 URL for your sample URL is:
http://maps.google.com/maps/api/staticmap?center=52.955115,-1.149172&zoom=11&size=512x512&markers=size:mid|color:red|label:A|52.955115,-1.149172&sensor=false
Notice these changes:
- Changed
/staticmap?
=>/maps/api/staticmap?
- Added
sensor=false
- Changed
markers
from52.955115,-1.149172,midreda
tosize:mid|color:red|label:A|52.955115,-1.149172
With all of the above the map displays correctly for me with the red marker, and should ensure Google recognizes your request as a V2 request.
精彩评论