google-mapsgoogle-maps-api-3google-geocoding-api

Google Maps JavaScript API throws "ExpiredKeyMapError" when Geocoding API is enabled


I use Google Maps JavaScript API for the map related matters and about to use Google Geocoding API to reverse the lat and lng to formatted address while users click on the map.

Everything works smoothly while I only use Maps JavaScript API until I enabled the Geocoding API. When I refreshed my page, I received error: Google Maps JavaScript API error: ExpiredKeyMapError.

To fix it is easy, simply disabled the Geocoding API fixed and apparently I can't use any features in Geocoding API anymore.

I also tried to generate a new API key and waited more than 2 hours but the problem is still there.

This is how I load the API and services

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=geocoding,places"></script>

How to properly use Maps JavaScript API and Geocoding API together?

Geocoding code to get address from lat and lng

var latLngToAddress = function (lat, lng) {
    var latLng = {lat: lat, lng: lng},
        geocoder = new google.maps.Geocoder;
    geocoder.geocode(
        {location: latLng},
        function (results, status) {
            console.log(results);
        }
    );
};

I get error Geocoding Service: This API project is not authorized to use this API. for doing above. If I enable the Geocoding service, then everything goes back to the original quesntion - the JS API will receive error ExpiredKeyMapError


I cannot guarantee the solution always work but this is how I solved it.

  1. Disable the Geocoding API
  2. Visit a Geocoding API request such as https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY and receive error.
  3. Enable the Geocoding API within the project.
  4. Visit a Geocoding API request again such as https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY and receive success.
  5. Refresh the project page and both JavaScript and Geocoding APIs work smoothly now.

Solution

  • Here is my solution to my question and this solution is not guaranteed to work in many other cases.

    1. Disable the Geocoding API
    2. Visit a Geocoding API request such as https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY and receive error.
    3. Enable the Geocoding API within the project.
    4. Visit a Geocoding API request again such as https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY and receive success.
    5. Refresh the project page and both JavaScript and Geocoding APIs work smoothly now.