问题
I have a JS application that is reading a URL which contains a city and state and geocoding those values to obtain a latitude and longitude. I am currently using the Google geocoding web service.
This works fine with no restrictions on my API key but since this is client side JS I am trying to set HTTP referrers with website restrictions. When I add my site as an HTTP referrer
(https://*.mysite.com/*)
the application breaks.
https://maps.googleapis.com/maps/api/geocode/json?address=city,+state
The only error I get is
TypeError: Cannot read property 'geometry' of undefined
because I am not returning any data.
Do I have to do something additional to use this client side with http referrers? I have no user input so I am trying not to use the Maps API since the google documentation says:
The Maps JavaScript API provides a geocoder class for geocoding and reverse geocoding dynamically from user input. If instead you wish to geocode static, known addresses, see the Geocoding web service.
回答1:
The Geocoding Web Service is meant to be used server side, not from JavaScript. You have to use IP Address restrictions on keys used with the web services. If you want to use HTTP referrers, you need to use the Google Maps JavaScript API v3.
来源:https://stackoverflow.com/questions/57100060/setting-http-referrers-for-googles-geocoding-web-service