【问题标题】:What is the proper way to get the Google Maps object from AngularJs?从 AngularJs 获取 Google Maps 对象的正确方法是什么?
【发布时间】:2016-05-11 15:53:00
【问题描述】:

完全披露:我对 JS 非常陌生,对 AngularJs 超级陌生。话虽如此...

我正在尝试制作一个使用 AngularJs 的路由提供程序功能的 Web 应用程序。我希望我使用的其中一个模板 URL 包含一个 Google Map 对象,当数据从后端服务器可用时,我可以动态添加标记等。

我的工作:

  • 多个模板 URL 和它们之间的正确导航(基于找到的出色教程 here
  • 我添加了另一个包含 ui-gmap-google-map 元素的模板 URL,并在模板 URL 的控制器内添加了一个通过 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)

我的新/更改文件如下:

pages/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() 以获取地图(根据文档),缺少什么?

【问题讨论】:

    标签: javascript angularjs google-maps


    【解决方案1】:

    似乎uiGmapGoogleMapApi promise 被解决并不意味着地图已经准备好。请改用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
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2010-10-03
      • 2011-04-26
      • 2015-11-12
      • 2017-01-28
      • 1970-01-01
      相关资源
      最近更新 更多