we are using the Google Maps API on our website to cross-check users entries of addresses.
A typical call would be: https://maps.googleapis.com/maps/api/geocode/json?key=XXXX&address=12-6 Daly St, Maryborough, VIC, Australia , 3465
Where XXXX is of course the key taken from the credentials.
We have set up the credentials in the developers console. Maps JavaScript API, Geocoding API, Geolocation API, and others are enabled.
The issue that we are having is that we are not able to restrict the key with "HTTP referrers (web sites)" nor with "IP addresses (web servers, cron jobs, etc.)". The result is that we have to leave the "Application Restriction" to "none".
As soon as we restrict this key, an error is returned:
With "HTTP referrers (web sites)", the error is:
"API keys with referer restrictions cannot be used with this API."
With "IP addresses (web servers, cron jobs, etc.)", the error is:
This IP, site or mobile application is not authorized to use this API key. Request received from IP address XXXXX
Could anyone help us in getting the right settings for this to work? We also have realised that the API fees have increased dramatically in the last month, which could indicate a theft of quota, making it imperative to restrict the keys.
Also, could there be another reason for the increase in fees for last month?
EXPECTED BEHAVIOUR: the API returns the JSON information with latitude, longitude etc.., which is working if we haven't set restrictions.
ACTUAL BEHAVIOUR: When adding the restriction though, the JSON response is an error with the message mentioned above.
UPDATE
I was calling the URL above with Javascript via ajax, having async set to false. I change the call following the description shown here: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple
Restrictions (HTTP referers) seem to be working fine now.
My issue is that I can return logs and values when address is correct, but nothing happens when the address is wrong.
I have the following in my code:
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY-KEY&callback=clf_init"></script>
<script>
var geocoder;
function clf_init() {
geocoder = new google.maps.Geocoder();
}
function submit(){
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address }, function(results, status)
{
console.log("STATUS: " +status);
console.log("RESULT: " + JSON.stringify(results));
if (status==='OK') {
console.log("SUCCESS");
}
else {
console.log("ERROR");
}
});
}
As mentioned, if the address is correct, I am getting the "STATUS: OK" log, but if the address is incorrect, I don't get any log at all, as if the function was not even executed. Any idea why this is and how I should change the code to handle error?