views:

955

answers:

4

Right now I can use this URL to request a Google Static Maps image successfully:

http://maps.google.com/staticmap?center=37.687,-122.407&zoom=8&size=450x300&maptype=terrain&key=[my key here]&sensor=false

However, the second I use JQuery or any direct javascript to set an image's src to the above url, Google passes back a Error 400:

"Your client has issued a malformed or illegal request."

I've read that this is usually from the key being incorrect, but my key is clearly being passed.

This is how I'm setting the image dynamically:

document.getElementById('my-image-id').src = "http://maps.google.com/staticmap?center=37.687,-122.407&zoom=8&size=450x300&maptype=terrain&key=[my key here]&sensor=false"

I've replaced [my key here] with my correct key, and it still doesn't work. When I request the same url through the browser, it's fine. I've confirmed that the correct referrers are getting passed as well.

Any ideas?

+1  A: 

You have a possible typo with the g on the end here:

&key=[my key here]g
lucas
Good catch, but that was a typo when I got rid of my key, it's not the source of the problem ;)
sotangochips
Haha. I figured as much :)
lucas
+3  A: 

Does this code work for you (it works for me -- be sure to insert your key)?

<html>
<head>
<script language="javascript">
    function swap() {
     document.getElementById('my-image-id').src = "http://maps.google.com/staticmap?center=37.687,-122.407&amp;zoom=8&amp;size=450x300&amp;maptype=terrain&amp;key=&amp;sensor=false"
    }
</script>
</head>
<body>
<img src="http://www.google.com/intl/en_ALL/images/logo.gif" width="450" height="300" onClick="swap();" id="my-image-id" />
</body>
</html>
lance
Ok, so the reason it wasn't working was that I was using the wrong API key. Apparently you don't need an API key when you're testing locally, so it wasn't failing. When trying it in production, I had the wrong API key and it *was* failing. Thanks for your help!
sotangochips
+1  A: 

The problem is with the key. Register for a new key and append with the url. It will work fine. I even was faccing same problem. It was working with direct paste in address bar of browser. But in production it was not working. I put new key and it worked fine both in production and browser.

kishor