Setting HTTP referrers for Google's geocoding web service

白昼怎懂夜的黑 提交于 2020-05-14 05:49:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!