Email message with static Google map not displaying correctly in Gmail
I am using C# code to generate an email message.
In the body of the message, I want to add a static Google map. To create the static URL, I have the following code
string addr = "111 Remote Drive Cary,NC 27511";
string mapU = "http://maps.google.com/maps/api/staticmap?center=" +
addr + HttpUtility.HtmlEncode("&zoom=14&size=512x512&maptype=roadmap&sensor=false&markers=color:green|") +
addr;
This string mapU is added to the 开发者_如何学JAVAmessage body as a src for an img tag.
When the email is sent to a Google Mail account, the static image is not displayed. Inspect element on broken image has
<img src="http://maps.google.com/maps/api/staticmap?center=111+Remote+Drive++Cary%2CNC+27511%26zoom%3D14%26size%3D512x512%26maptype%3Droadmap%26sensor%3Dfalse%26markers%3Dcolor:green%7C111+Remote+Drive++Cary,NC+27511">
Gmail is replacing '=' with %3D, which seems to be breaking this.
Sending the exact same message to Hotmail works. The image is displayed in the correct place as expected. Hotmail is keeping '=' intact.
Any ideas what is going on with Gmail? Suggestions how this could be fixed?
Thanks Abhi
It looks like your encoding is a bit off - you want to encode individual values, not the entire query string.
More importantly, Google leads here for the term "google maps static gmail", so I wanted to also offer this advice:
- To include a static map and have it show in GMail, you must use an API key
e.g.
<img src="https://maps.googleapis.com/maps/api/staticmap?key=KEY&sensor=false&size=200x200&zoom=12&markers=0,0" />
I wish I had the doc to support that (if it exists) or even the link to the thread where I first read it so I could give proper attribution - please edit if you know. In either case, it worked for me!
Maybe for some reason the URL encode is not working correctly.. or is causing gmail to freak out about something...
Remove the encode and manually replace the & with & and it should be fine..
Sorry, not the most elegant solution...
EDIT:
Actually I don't know if it matters or not... but your not encoding the first = maybe encode the entire URL after you have built it... :-)
精彩评论