2

While attempting to embed a Google Maps (API v3) widget inside an Angular2 app, I discovered that there's a click/drag error that disturbs me.

I want to know if there's a good way I can embed the Google Map component it is isolated from Angular. Based on the stacktrace (below), it looks like Google Maps reaches an unexpected condition after Zone attempts to invoke a task.

Simple Plunkr example (runnable): https://plnkr.co/edit/Tg3zwphygrgLUG5mq8Cc

Backtrace after each bug click (in README.md):

- Angular: angular2-polyfills.js:324 Uncaught TypeError: Cannot read property 'Ra' of null
- Google Maps: _.r.jl @ common.js:244
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: fx @ common.js:190
- Google Maps: (anonymous function) @ common.js:189
- Google Maps: _.J.trigger @ js?key=NICE_TRY_HACKERS&callback=initMap:103
- Google Maps: _.r.Mm @ common.js:257_.r.Kn @ common.js:231
- Angular: ZoneDelegate.invokeTask @ angular2-polyfills.js:423
- Angular: Zone.runTask @ angular2-polyfills.js:320
- Angular: ZoneTask.invoke @ angular2-polyfills.js:490

HTML (index.html):

        <!--
            Google Maps alongside Angular2
            =======================================
            See README.md
    -->
    <html>
    <head>
            <title>Angular 2 QuickStart</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">

            <!-- 1. (Angular2) Load libraries -->
            <!-- IE required polyfills, in this exact order -->
            <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.0/es6-shim.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system-polyfills.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2-polyfills.min.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.26/system.src.js"></script>
            <script src="https://npmcdn.com/rxjs@5.0.0-beta.2/bundles/Rx.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.15/angular2.min.js"></script>

            <!-- 2. (Angular2) Configure SystemJS -->
            <script>
                    System.import('main.js').then(null, console.error.bind(console));
            </script>

    </head>

    <!-- 3. (Angular2) Display the application -->
    <body>
    <my-app>Loading...</my-app>

    <!-- 4. GOOGLE MAPS START, source: https://developers.google.com/maps/documentation/javascript/tutorial#The_Hello_World_of_Google_Maps_v3-->

            <style>
                    html, body, #map {
                            height: 100%;
                            margin: 0;
                            padding: 0;
                    }
            </style>

            <div id="map"></div> <!-- Google Map goes here -->

            <script>
                    var map;
                    function initMap() {
                            map = new google.maps.Map(document.getElementById('map'), {
                                    center: {lat: -34.397, lng: 150.644},
                                    zoom: 8
                            });
                    }
            </script>

            <script src="https://maps.googleapis.com/maps/api/js?key=NOTHING_TO_SEE_HERE&callback=initMap" async defer></script>

    <!-- GOOGLE MAPS END -->

    </body>
    </html>

Angular2 Bare-Bones App (main.ts)

import {bootstrap}    from 'angular2/platform/browser';
import {Component} from 'angular2/core';

// Create Angular component
@Component({
    selector: 'my-app',
    template: '<h1>My First Angular 2 App</h1>'
})
export class AppComponent { }

// Bootstrap the component
bootstrap(AppComponent);

Observations:

  • Every click triggers an error
  • Error seems to be non-critical, but best not to proceed on shaky ground
  • Stacktrace suggests Google Maps (_.r.jl @ common.js:244) is surprised by a task invoked by Angular2's Zone (ZoneDelegate.invokeTask @ angular2-polyfills.js)
  • The widget works in a bare-bones app, but complains in Angular
Langley
  • 5,326
  • 2
  • 26
  • 42
montooner
  • 1,112
  • 4
  • 17
  • 29

1 Answers1

1

There are few 3rdparty solutions that provide ready to use components like https://angular-maps.com/.

kemsky
  • 14,727
  • 3
  • 32
  • 51
  • That works. In general, what techniques are available if you wanted to troubleshoot it yourself? I imagine there are times where there are no 3rd party solutions. – montooner Apr 19 '16 at 08:05
  • i guess there is no silver bullet, angular heavily relies on zone.js change detection and polyfills, most issues come from these parts. – kemsky Apr 19 '16 at 08:42
  • Ah, OK. So it's not uncommon for zone.js to be noisy. That's good to know. – montooner Apr 19 '16 at 21:32