[英]Google Map isn't loading - Angular JS
I'm trying to make an application with Google Map, however, it seems I'm missing out something. 我正在尝试使用Google Map创建应用程序,但是似乎缺少了一些东西。
Here is my code: 这是我的代码:
<div class="container-fluid" ng-app="mapApp" ng-controller="mapCtrl">
<div class="row">
<div class="container-fluid">
<div class="row header">
<ui-gmap-google-map center='map.center' zoom='map.zoom'>{{map}}</ui-gmap-google-map>
</div>
</div>
</div>
</div>
app.js: app.js:
'use strict';
// Declare app level module which depends on views, and components
angular.module('mapApp', [
'ngRoute'
]).config(['locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
$locationProvider.hashPrefix('!');
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
.config(function(uiGmapGoogleMapApiProvider) {
uiGmapGoogleMapApiProvider.configure({
key: 'key',
v: '3.20', //defaults to latest 3.X anyhow
libraries: 'weather,geometry,visualization'
});
});
controller.js: controller.js:
app.controller('mapCtrl', function($scope, uiGmapGoogleMapApi) {
//make magic
uiGmapGoogleMapApi.then(function(maps) {
$scope.map = { center: { latitude: 45, longitude: -73 }, zoom: 8 };
});
});
and I have added style: 并且我添加了样式:
.angular-google-map-container {
height: 400px; }
The output doesn't seem to parse the expression well, so I only receive {{map}}
. 输出似乎不能很好地解析表达式,因此我只收到{{map}}
。
I have lost entire day, trying to figure this one out.. it is probably something 'small' I have missed. 我整天迷路了,试图弄清楚这件事。.可能是我错过的“小”事情。 Please note, that under 'key' there is a legit API key. 请注意,在“密钥”下有一个合法的API密钥。
Please help, thanks. 请帮忙,谢谢。
Put the $scope.map outside the promise in controller. 将$ scope.map放在控制器中的Promise之外。 With the current set up you should get an eror - "angular-google-maps: a center or bounds property is required". 在当前设置下,您应该会得到一个错误-“ angular-google-maps:需要center或bounds属性”。
app.controller('mapCtrl', function($scope, uiGmapGoogleMapApi) {
$scope.map = {center: { latitude: 45, longitude: -73 }, zoom: 8 };
});
A plunkr that works - here - provide a API KEY 有效的插件- 在此处提供API密钥
You also need to declare a dependency on the uiGmapgoogle-maps
module, so replace 您还需要声明对uiGmapgoogle-maps
模块的依赖关系,因此请替换
angular.module('mapApp', [
'ngRoute'
])
with 同
angular.module('mapApp', [
'uiGmapgoogle-maps',
'ngRoute'
])
There are a few typo issues in the provided example, once they are are fixed too, the map should be loaded as excepted 提供的示例中存在一些拼写错误,一旦它们也得到修复,则应按例外方式加载地图
'use strict'; angular.module('mapApp', [ 'uiGmapgoogle-maps', 'ngRoute' ]) .config(['$locationProvider', '$routeProvider', function ($locationProvider, $routeProvider) { //$locationProvider.hashPrefix('!'); //$routeProvider.otherwise({ redirectTo: '/view1' }); }]) .config(function (uiGmapGoogleMapApiProvider) { uiGmapGoogleMapApiProvider.configure({ //key: '', v: '3.20', //defaults to latest 3.X anyhow libraries: 'weather,geometry,visualization' }); }) .controller('mapCtrl', function($scope, uiGmapGoogleMapApi) { uiGmapGoogleMapApi.then(function(maps) { $scope.map = { center: { latitude: 45, longitude: -73 }, zoom: 8 }; }); });
.angular-google-map-container { height: 400px; }
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.0.1/lodash.js" type="text/javascript"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.10/angular.js"></script> <script src="http://cdn.rawgit.com/nmccready/angular-simple-logger/0.0.1/dist/index.js"></script> <script src="http://rawgit.com/angular-ui/angular-google-maps/2.3.2/dist/angular-google-maps.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.10/angular-route.min.js"></script> <div class="container-fluid" ng-app="mapApp" ng-controller="mapCtrl"> <div class="row"> <div class="container-fluid"> <div class="row header"> <ui-gmap-google-map center='map.center' zoom='map.zoom'>{{map}}</ui-gmap-google-map> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.