Starting July 16, 2018, the Google Maps API is no longer completely free.
After July 16, 2018, in order to continue utilizing the Google Maps Platform APIs, you must activate billing for each of your projects.
(https://developers.google.com/maps/documentation/javascript/usage-and-billing).
Since implementing the new Google Maps API along with the required key and billing information a week ago, I have noticed significant charges for my usage. This indicates high traffic to my website, which is positive. However, the issue arises from the fact that most of my visitors may not necessarily view or interact with the maps all the time, yet I am charged as soon as a page containing a map loads.
My solution involves not displaying the map by default, but instead providing a "Show map" link so that only users who are genuinely interested can view it. I intend to make an AJAX call to the Google Maps API when necessary. While I acknowledge that this approach may seem like a way to avoid charges, I believe it is fair as I only wish to be billed when visitors actively engage with the Google Maps feature and not just when pages load. Would Google consider this approach acceptable or view it as cheating? Refer to the code at https://developers.google.com/maps/documentation/javascript/tutorial. The plan is to trigger this code block via an AJAX request when users click a "Show map" button in order to reduce costs:
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
UPDATE 1: In light of reading Executing <script> inside <div> retrieved by AJAX, particularly the response by Chocula:
JavaScript added as DOM text will not execute.
Considering this, attempting the method I have described may not be successful, as JavaScript inserted as DOM text does not run. While it seems like a clever way to avoid charges in cases where users do not interact with the map, it may not work in this scenario with AJAX. Inserting the <script>...</script>
block through AJAX would result in it not executing, thus potentially invalidating the strategy. Immediate billing would occur regardless of whether the map is displayed by showing the <div id="map"></div>
section. While it is an intriguing method to bypass payments fairly, I have doubts about its feasibility with AJAX.