Rober
Rober

Reputation: 6108

Google Maps API REQUEST_DENIED error depending on address

I´m integrating a Google Maps with the API in my AngularJS app using a directive.

Sample code:

HTML:

<boats-map location="Mallorca, Islas Baleares, España" latitude="" longitude="" zoom="8" class="map-container" style="height: 300px; margin-bottom: 20px"></boats-map>

And this is my maps initialization:

<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false" defer></script>

JS:

... # I don´t put all the directive code, because I think it´s not relevant. Let me know if you need it
geocoder.geocode({'address': attrs.location}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) { 
                    var location = results[0].geometry.location;

The point is that it works sometimes and sometimes not depending on the address. Working example: location="Calle Noruega, 6 Palma de Mallorca, Islas Baleares, España"

NOT Working example: location="Mallorca, Islas Baleares, España" with status code= REQUEST_DENIED.

Upvotes: 1

Views: 582

Answers (1)

rags2riches-prog
rags2riches-prog

Reputation: 1761

The point is that it works sometimes and sometimes not depending on the address.

You nailed the very essence of the Geocoding Service: asynchronous requests made to an external server that may return more than one result.

The point I am trying to make here is that we are assuming the problem is the code when in fact it may not necessarily be so. If you have tested the same input address in succession, within rapid intervals, and the server returns always an error, this is because Geocoding does not "like" ambiguous, incomplete queries and possibly the servers are not finding the address you are typing in. This is highly unlikely even though Google warns in their documentation about Location Biasing and also about using the Autocomplete Service when working with incomplete address. (This is also because Geocoding is resource intensive and put some workload on servers, an aspect that you want to consider as important if you are building an application that is sensitive to latency issues). If I was you, I would not rule out a network issue as well because as you stated above, sometimes you get an error, sometimes you do not.

Next, you code:

... # I don´t put all the directive code, because I think it´s not relevant.

Why not including 1 or 2 console logs between your geocoder initializer and the if statement?

geocoder.geocode({'address': attrs.location}, function(results, status) {
console.log(results);
console.log(results.length); 
  if (status == google.maps.GeocoderStatus.OK) { 
  var location = results[0].geometry.location;

Next solution you should try is to check if you API key has been correctly implemented in your Google console; the error you are getting is at times thrown because of incorrect implementation.

Alternatively, you should really include a Minimal, Complete, and Verifiable example so that I could try to reproduce the problem you are having.

Upvotes: 2

Related Questions