The system encountered an error due to the absence of a defined Google or a MissingKeyMapError from the

Currently, I am developing a component that includes ng-map functionality by following the guidance in this tutorial.

<div class="content-pane" map-lazy-load="https://maps.google.com/maps/api/js" map-lazy-load-params="{{$ctrl.googleMapsUrl}}">
    <ng-map class="content-pane" center="41,-87" zoom="5"> 
    ...
    </ng-map>
</div>

While working on my controller, I encountered a common issue like this:

var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < this.positions.length; i++) {
    var latlng = new google.maps.LatLng(this.positions[i][0], this.positions[i][1]);
    bounds.extend(latlng);
}

This led to an error stating 'google is not defined' as shown below:

angular.js:13708 ReferenceError: google is not defined
    at new app.component.controller (map.component.js:13)
    at Object.invoke (angular.js:4709)
    at $controllerInit (angular.js:10234)
    at nodeLinkFn (angular.js:9147)
    at angular.js:9553
    at processQueue (angular.js:16170)
    at angular.js:16186
    at Scope.$eval (angular.js:17444)
    at Scope.$digest (angular.js:17257)
    at Scope.$apply (angular.js:17552)

After some online research, I came across a related bug report in this link. The suggestion was to refer back to the tutorial example I had been using to resolve the issue, as it included script-tags-for-development.js which eagerly loads the required script. However, implementing this solution did not fully resolve the problem.

document.write([
  '<script src="https://maps.google.com/maps/api/js?libraries=placeses,visualization,drawing,geometry,places"></script>',
...

Adding this script to the HTML resolved one issue but introduced another - 'Google Maps API error: MissingKeyMapError'. It became apparent that hardcoding the API key into the HTML head was not the ideal approach for me, as I required configurability within my Angular application.

To address the 'missing API key' error, I followed a helpful tutorial suggesting the use of a lazy-load directive. However, this brought me back to the initial problem I faced.

If you need to pass in an API key to the javascript, you can set a scope variable in your controller (e.g. $scope.googleMapsUrl="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY_HERE";). This can be set from a constant value in your app to standardise the API key to pass to google for multiple controllers.

<div map-lazy-load="https://maps.google.com/maps/api/js"
  map-lazy-load-params="">
  <ng-map center="41,-87" zoom="3"></ng-map>
</div>

Therefore, my question remains: How do the official ng-map tutorials/examples function without requiring a Google Maps API key? And how can I successfully overcome the 'google not found' error when utilizing the lazy-load directive instead of hardcoding the API key in the HTML head?

Answer №1

The issue I encountered stemmed from the presence of the async attribute in the script tag:

<script src="https://maps.googleapis.com/maps/api/js?key=xxx" async defer></script>

By removing the async attribute, everything started functioning correctly. It is important to note that by doing so, the script now blocks while loading:

The async attribute allows the browser to render the rest of your website while the Maps JavaScript API loads. Once the API is ready, it executes the specified function using the callback parameter.

For more details, refer to https://developers.google.com/maps/documentation/javascript/tutorial?hl=en#Loading_the_Maps_API

Another suggestion is to utilize ngMap's lazy-loading feature:

<div map-lazy-load="https://maps.google.com/maps/api/js"
     map-lazy-load-params="{{googleMapsUrl}}">
  <ng-map center="41,-87" zoom="3"></ng-map>
</div>

Explore further at https://github.com/allenhwkim/angularjs-google-maps#lazy-loading-of-google-maps-javascript

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determining the number of words in every line within a textarea

I am looking to determine the number of words per line in a textarea. The width of the textarea is variable. Check out this code snippet that calculates the number of rows: http://jsfiddle.net/2tcygj9e/ ...

Check for input validation with jQuery when the element has a specific class

Utilizing the jQuery validation plugin for a checkout form on an ecommerce platform has proven to work excellently. However, I am in need of validating only those inputs that do not possess the class no-validate. Would it be possible to make use of the de ...

The process of examining a function that includes the invocation of an additional API function in a NodeJS environment

I'm faced with a situation where I have a nested function inside another function. The second function is responsible for making an API call. However, in order to write a unit test for this scenario, I need to avoid actually making the real API call a ...

Issue with ng-disabled not functioning properly for href tag within list item

Is there a way to prevent clicking on the <a> tag within a list item in a UI list so that the associated <div> is not displayed when clicked (excluding the last list item)? I have attempted using ng-disabled directly on the list item attribute ...

Alert-Enabled Random Number Generation Tool

I'm struggling to create a program that randomly generates a number between 1 and 20. If the number is less than 10, it should display "fail," and if it's greater than 10, it should display "success." I can't seem to figure out where I went ...

I am looking to retrieve data from the Graph API JSON and gradually refine my search to achieve successful

I am looking to retrieve data from the "fb_page_categories" endpoint, which provides an array of categories a page can be categorized under. The format for this request is as follows: GET graph.facebook.com /fb_page_categories? Once this request is mad ...

Tips on postponing the loading of an iframe within a collapsed div using jQuery until the div/button is clicked

So I'm dealing with a bunch of these collapsible divs on a single page, each containing an iframe. (I used some CSS to make it look like a button when clicked). The issue is that the page is loading very slowly because it's loading all the domai ...

What could be the reason my dropdown menu is not appearing on hover?

Currently, I am in the process of developing a menu using angularJS and google app script within a dialog box. I have been referring to this sample code for guidance. Instead of pasting all my code here, I can summarize what I have come up with: var a ...

IIFE and the Twitter share button are a powerful duo

As I experiment with this quick creation of mine, two questions have arisen. The first one is, how can I encapsulate all the JS code within an IIFE without breaking it? The second question is about finishing the twitter button to enable sharing the act ...

I am looking to integrate a custom button that, when clicked, will launch the file explorer for me to choose a file. Once selected, the file name should automatically populate in the input field

In the code below, when the button is clicked, Windows Explorer should open and allow the user to select a file. The selected file should then be displayed in the input field. The file type should not be 'File'. <Grid.Column width={8}> ...

Updating the value of the variable using ng-submit

My application displays Quantity: {{num}}. The default value is set to 3 in the scope. The objective is to click on: <form ng-submit="addContact()"> <input class="btn-primary" type="submit" value="Add Contact"> </form> and to up ...

Guide on automatic session logout on one computer when the user is logged in on another computer

Currently working with nodejs, angularjs and session to develop my website. I am interested in implementing a feature where a user can only be logged in on one device at a time. For example, if the user is logged in on computer A and then logs in on comp ...

Unable to pass the $rootScope variable to the following function in AngularJS 1.x

I'm facing an issue that I can't seem to solve. It appears that a variable in $rootScope is not being assigned when I call the next function, but isn't that the purpose of a Promise (.then)? I am using AngularJS v1.6.4 and NodeJS, however, ...

Does anyone know of a convenient tool that can automatically provide suggested street names for mailing addresses in every country across the globe, for free?

Is there a convenient tool that automatically completes street addresses for various countries worldwide? I'm considering options similar to YQL from Yahoo or Foursquare. I'd like to provide users with suggestions of known street names as they ...

Having trouble displaying child nodes in MatTreeView with Angular 14?

In an Angular project, I am attempting to display a private group's data from GitLab (utilizing a public one for testing purposes). To achieve this, I have implemented the NestedTreeView component. While the parent nodes are displaying correctly, I am ...

Utilizing Angular to incorporate a JSON file within a form

Recently, I've delved into the world of angular and node.js. My current challenge involves populating html forms with content from a json file based on dropdown selection. While I've successfully achieved this, I encounter an issue when manually ...

Masonry.js is working perfectly in Firefox, however, it is experiencing compatibility issues with Chrome and

Recently, I encountered a strange issue with Dessandro's Masonry.js library. It was working perfectly fine until it suddenly stopped functioning in Safari and Chrome (I haven't tested it on IE yet), while still working flawlessly in Firefox. Usua ...

Refine your search with a JSON object description in expressJS and UnderscoreJS

[ { "id": 1, "description": "Empty the garbage bin", "completed": false }, { "id": 2, "description": "Dine out for dinner", "completed": false }, { "id": 3, "description": "Exercise at the fitness center", "com ...

I'm still searching for a proper solution on how to access JavaScript/jQuery functions within Colorbox

For my website, I am utilizing PHP, jQuery/JavaScript, Colorbox (a jQuery lightbox plugin), Smarty, and other tools. Currently, I am working on displaying data in a popup using the Colorbox plugin. However, I am facing an issue with calling a JavaScript fu ...

The span's onclick function seems to be malfunctioning

I am encountering an issue where the Onclick event is not triggering on a specific tag. Strangely, the same onclick event works perfectly fine when bound to an image element. I am currently developing a follow and unfollow feature using PHP and jQuery. How ...