完全披露:我对 JS 非常陌生,对 AngularJs 也非常陌生。话虽如此...
我正在尝试制作一个使用 AngularJs 的路由提供程序功能的 Web 应用程序。我希望我正在使用的 templateURL 之一包含一个 Google map 对象,当数据从后端服务器可用时,我可以动态地添加标记等。
我正在做的事情:
- 多个模板网址以及它们之间的正确导航(基于发现的精彩教程 here 。
- 我添加了另一个模板 URL,其中包含
ui-gmap-google-map
元素和模板 URL 的 Controller 内部,我通过uiGmapGoogleMapApi.then
添加了一个回调正在被调用( map 可见)。
缺少什么:
- 我无法访问底层 Google map 对象,我应该在其中添加标记、折线等。
- 我已阅读文档 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()
,还缺少什么?为了获取 map (根据文档)?
最佳答案
似乎当 uiGmapGoogleMapApi
promise 得到解决时,并不意味着 map 已经准备好。尝试使用 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
});
});
关于javascript - 从 AngularJs 获取 Google Maps 对象的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37167672/