Google static map API getting 403 forbidden when loading from img tag

后端 未结 11 693
执念已碎
执念已碎 2021-01-03 21:15

What I have is a Google map that shows the location of a property but when I come to print the dynamic maps dont print so good so I decided to implement the Google Static Ma

相关标签:
11条回答
  • 2021-01-03 21:21

    There seems to be some confusion here, and since this thread is highly ranked on Google, it seems relevant to clarify.

    Google has a couple of different API's to use for their maps service:

    Javascript API

    The old version of this API was version 2, which required a key. This version is deprecated, and it is recommended to upgrade to the newer version 3. Note that the documentation still states that you need a key for this to function, except if you're using "Google Maps API for Business".

    Static Maps API

    This is a whole different story. Static maps is a service that does not require any javascript. You simply call an url, and Google will return a maps image, making it possible to insert the URL directly into your <img> tag. The newest version is version 2, and this requires a key to function because a usage limit is applied.

    A key can be requested here: https://code.google.com/apis/console

    And the key should be added to the request for the correct image to be generated:

    http://maps.googleapis.com/maps/api/staticmap?center=New+York,NY&zoom=13&size=600x300&key=API_console_key

    I hope this clears up some confusion.

    0 讨论(0)
  • 2021-01-03 21:24

    Staticmaps V3 doesn't need the "Key" attribute and removing it seems to solve the <img> source problem.

    Try with an URL like this:

    http://maps.googleapis.com/maps/api/staticmap?center=0.0000,0.0000&zoom=13&size=200x200&maptype=roadmap&markers=0.0000,0.0000&sensor=false
    

    For more information read this.

    0 讨论(0)
  • 2021-01-03 21:27

    Try enabling billing on this Google Cloud Project/Firebase Project.

    I was experiencing this same issue and just received the 403 error in the console. Copying and pasting the Static Maps URL in to the URL bar and loading it showed the following error message:

    The Google Maps Platform server rejected your request. You must enable Billing on the Google Cloud Project at 
    https://console.cloud.google.com/project/_/billing/enable Learn more at https://developers.google.com/maps/gmp-get-started
    

    Hope this helps!

    0 讨论(0)
  • 2021-01-03 21:28

    I could also not display static maps and could see 403 error in the browser's network console.

    http response headers:

    status:403
    x-content-type-options:nosniff
    

    I had an API key with a lot of Google Maps APIs enabled but the Google Static Maps API was missing, enabling it solved the issue.

    0 讨论(0)
  • 2021-01-03 21:30

    Yeah, Google Maps API version 3 were java-script version; "Google Static Maps" latest were 2.0. I suspect there might be some restriction on use.

    0 讨论(0)
  • 2021-01-03 21:30

    now you should use 'signature' parameter, which you should add to request - otherwise static maps won't work. here is few useful links

    1) how to generate signature

    2) how to make signature on BE side (code snippet)

    0 讨论(0)
提交回复
热议问题