从AngularJs获取谷歌地图对象的正确方法是什么

What is the proper way to get the Google Maps object from AngularJs?

本文关键字:方法 是什么 对象 AngularJs 获取 谷歌地图      更新时间:2023-09-26

全面披露:我对JS和SUPER都是新手。话虽如此。。。

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

我的工作内容:

  • 多个模板URL和它们之间的正确导航(基于这里的伟大教程)
  • 我添加了另一个包含ui-gmap-google-map元素的模板URL,在模板URL的控制器中,我通过正在调用的uiGmapGoogleMapApi.then添加了一个回调(映射是可见的)

缺少什么:

  • 我无法访问要添加标记、多段线等的底层谷歌地图对象
  • 我已经阅读了这里的文档,其中说要将control作为HTML标记的一部分(请参阅下面的map.html文件),并看到了这个很有希望的答案,它似乎解决了我的确切问题。但是,我的$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()来获得地图(根据文档),还缺少什么呢?

uiGmapGoogleMapApi promise被解析时,似乎并不意味着映射已经准备好。请尝试uiGmapIsReady.promise()(有关更多信息,请查看此SO答案)。工作代码:

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