从AngularJs获取Google Maps对象的正确方法是什么?

时间:2016-05-11 15:53:01

标签: javascript angularjs google-maps

完全披露:我是JS的新手,也是AngularJs的新手。随着那说......

我正在尝试创建一个使用AngularJs的Web应用程序。路由提供商功能。我想使用其中一个templateURL来包含一个Google Map对象,我可以动态地添加标记等,因为后端服务器可以提供数据。

我的工作:

  • 多个模板网址和它们之间的正确导航(基于找到的精彩教程here
  • 我添加了另一个包含ui-gmap-google-map元素的模板网址,并在控制器内部为模板网址添加了一个回调,我通过uiGmapGoogleMapApi.then添加了一个被调用的回调(地图可见)。

缺少什么:

  • 我无法访问我应该添加标记,折线等的基础Google地图对象。
  • 我已阅读文档here,其中包含control作为HTML标记的一部分(请参阅下面的map.html文件)并看到this promising answer似乎解决我的确切问题。但是,我的$scope.map.control对象永远不会被填写(有关详细信息,请参见下文)。

我的设置如下:

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)

我的新/更改文件如下:

页/ map.html

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

index.html(主要是教程代码)

<!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>

最后是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");
        }
    });
});

那么为了让我获得控制权以便我可以调用$scope.map.control.getGMap()来获取地图(根据文档)会缺少什么?

1 个答案:

答案 0 :(得分:1)

似乎当uiGmapGoogleMapApi承诺得到解决时,并不意味着地图已准备就绪。请尝试使用uiGmapIsReady.promise()(有关详情,请查看this SO answer)。工作代码:

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
        });
    });