0

Full disclosure: I am really new to JS and SUPER new to AngularJs. With that said...

I am trying to make a web application that uses AngularJs' route provider functionality. I would like one of the templateURLs that I am using to include a Google Map object that I can add markers, etc. to dynamically as data becomes available from a backend server.

What I have working:

  • Multiple template URLs and proper navigation between them (based on the great tutorial found here.
  • I added another template URL that contains the ui-gmap-google-map element and inside the controller for the template URL, I added a callback via uiGmapGoogleMapApi.then that is being called (the map is visible).

What is missing:

  • I cannot get access to the underlying Google Maps object where I am supposed to add markers, polylines, etc.
  • I have read the documentation here that says to include a control as part of the HTML markup (see my map.html file below) and saw this promising answer that seems to address my exact question. However, my $scope.map.control object is never filled out (see below for more details).

My setup is as follows:

webapp
--css
  --style.css // contains ".angular-google-map-container {height: 400px;}"
--js
  --angular-google-maps.min.js
  --angular-simple-logger.js
  --lodash.min.js
--pages
  --about.html // from tutorial
  --contact.html // from tutorial
  --home.html // from tutorial
  --map.html // my map is in here (see below)
--WEB-INF
  --web.xml
--index.html // modified from the tutorial (see below)
--script.js // modified from the tutorial (see below)

My new/changed files are as follows:

pages/map.html

<div>
  <ui-gmap-google-map center='map.center' zoom='map.zoom' control='map.control'></ui-gmap-google-map>
</div>

index.html (mostly just the tutorial code)

<!DOCTYPE html>
<html ng-app="scotchApp">
<head>
    <!-- SCROLLS -->
    <!-- load bootstrap and fontawesome via CDN -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
    <link rel="stylesheet" href="css/style.css"/>

    <!-- SPELLS -->
    <!-- load angular and angular route via CDN -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
    <script src="js/lodash.min.js"></script>
    <script src="js/angular-google-maps.min.js"></script>
    <script src="js/angular-simple-logger.js"></script>
    <script src="script.js"></script>
</head>
<body>

<!-- HEADER AND NAVBAR -->
<header>
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand" href="/">Angular Routing Example</a>
            </div>

            <ul class="nav navbar-nav navbar-right">
                <li><a href="#"><i class="fa fa-home"></i> Home</a></li>
                <li><a href="#about"><i class="fa fa-shield"></i> About</a></li>
                <li><a href="#contact"><i class="fa fa-comment"></i> Contact</a></li>
                <li><a href="#map"><i class="my_location"></i> Map</a></li>
            </ul>
        </div>
    </nav>
</header>

<!-- MAIN CONTENT AND INJECTED VIEWS -->
<div id="main">

    <!-- angular templating -->
    <!-- this is where content will be injected -->
    <!--{{ message }}-->
    <div ng-view></div>

</div>

</body>
</html>

And finally script.js

// create the module and name it scotchApp
var scotchApp = angular.module('scotchApp', ['ngRoute', 'uiGmapgoogle-maps']);

scotchApp.config(function($routeProvider) {
    $routeProvider           

        // ...
        // Skipping unrelated routing code from the tutorial
        // ...

        // route for the map page (THIS IS NEW)
        .when('/map', {
            templateUrl : 'pages/map.html',
            controller  : 'mapController'
        });
}).config(function (uiGmapGoogleMapApiProvider) {
    uiGmapGoogleMapApiProvider.configure({
        key: '{KEY IS HERE}',
        v: '3.20',
        libraries: 'weather,geometry,visualization'
    });
});

// ...
// Skipping unrelated controller code from the tutorial
// ...

scotchApp.controller('mapController', function($scope, uiGmapGoogleMapApi) {
    // Here is my empty "control" as specified by the documentation
    $scope.map = { control: {}, center: { latitude: 45, longitude: -73 }, zoom: 8 };
    uiGmapGoogleMapApi.then(function(maps) {
        console.log("Maps are ready");

        // Here $scope.map.control is still empty. What gives?
        if ($scope) {
            console.log("Scope is defined");
        }
        else {
            console.log("Scope is not defined");
        }
    });
});

So what is missing in order for me to get the control so that I can then call $scope.map.control.getGMap() in order to get the map (per the documentation)?

Community
  • 1
  • 1
curob
  • 705
  • 4
  • 17

1 Answers1

1

It seems that when uiGmapGoogleMapApi promise is resolved it doesn't mean that the map is ready. Try uiGmapIsReady.promise() instead (for more info check this SO answer). Working code:

scotchApp.controller('mapController', function($scope, uiGmapGoogleMapApi, uiGmapIsReady) {
        // Here is my empty "control" as specified by the documentation
        $scope.map = { control: {}, center: { latitude: 45, longitude: -73 }, zoom: 8 };

        uiGmapIsReady.promise().then(function(instances) {
            console.log($scope.map.control.getGMap); //is set now
        });
    });
Community
  • 1
  • 1
Matej P.
  • 5,303
  • 1
  • 28
  • 44