How do I use an API key with Google Static Maps?

I'm trying to work with google static maps but I keep getting 403 errors and have had much confusion trying to figure this out. If I right-click on the broken image and choose "open in new tab" then it will load fine, and after that it will also load fine on the actual page where I'm embedding it. I'm not sure why it is working this way.

I've read that 403 means its hit some quota limit, although this is a fairly low-traffic server. I tried setting up an API key but I'm rather confused as to how it is supposed to work. Google's documentation for static maps says you need to create a "Server Key" but also that this key should be kept private on the server and not embedded on the page. How do I use the server key without putting it in the source of the image tag?

I've read elsewhere on stackoverflow that I should use a "Browser Key" instead. This seems to work okay but there are many domains that I intend to use this on and I can't put all of them in the allowed referrers. Should I just let it allow any referrer?

I'm using the V2 API with urls like this: http://maps.googleapis.com/maps/api/staticmap?center={someaddress}&size=250x250&zoom=14&key={mykey}


You can use a browser key with static maps, so you don't need a server key. You have multiple options here if you want to use a browser key on multiple domains.

It is possible to just allow all referrers on a browser key, but then you run the risk that other people start using your API key as well. If you haven't setup billing, then the highest risk you take is that the images become unavailable because somebody else used your key and therefore you hit the quota.

The second option is to use a separate browser API key per domain. Then you can set the allowed referrers per domain.


I eventually managed to figure out the cause of the error. I was including a custom map marker in the url and it turned out the server could be a bit slow in serving the marker image to google. Google seems to have a very low timeout while waiting for the map marker, so it would give up waiting and return an error. I removed the custom marker and now it works fine, without the need to use an API key.

链接地址: http://www.djcxy.com/p/67542.html

上一篇: ImageView显示静态谷歌URL地图。 点击图片时如何启动地图

下一篇: 如何在Google静态地图上使用API​​密钥?