如何在Angularjs控制器中使用初始谷歌地图 [英] How use initial google map in Angularjs controller
本文介绍了如何在Angularjs控制器中使用初始谷歌地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的角度控制器中实现以下谷歌示例。
https://developers.google.com/maps/documentation/javascript/examples/map-simple
< html>
< head>
< title>简易地图< /标题>
< meta name =viewportcontent =initial-scale = 1.0>
< meta charset =utf-8>
< style>
html,body {
height:100%;
保证金:0;
padding:0;
}
#map {
height:100%;
}
< / style>
< / head>
< body>
< div id =map>< / div>
< script>
var map;
函数initMap(){
map = new google.maps.Map(document.getElementById('map'),{
center:{lat:-34.397,lng:150.644},
zoom:8
});
}
< / script>
< script src =https://maps.googleapis.com/maps/api/js?callback=initMap
async defer>< / script>
< / body>
< / html>
其中google map api的加载需要在窗口范围内跟随一个initMap。
角度控制器应该怎么做?以下代码无效:
angular.module('testApp')
.controller('MainCtrl',function ($ scope,$ window){
$ window.map;
$ window.initMap = function(){
$ window.map = new google.maps.Map( document.getElementById('map'),{
center:{lat:-34.397,lng:150.644},
zoom:8
});
}
});
解决方案
添加API密钥并删除callback = initMap:
< script src =https://maps.googleapis.com/maps/api/js?callback=initMap async defer>< / script>
I would like to implement below google sample inside my angular controller.
https://developers.google.com/maps/documentation/javascript/examples/map-simple
<html>
<head>
<title>Simple Map</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
</body>
</html>
Wherein the load of google map api needs a initMap to be followed inside the window scope.
How should I do inside angular controller? Code below does not work:
angular.module('testApp')
.controller('MainCtrl', function ($scope, $window) {
$window.map;
$window.initMap = function() {
$window.map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 8
});
}
});
解决方案
Add your API key and remove callback=initMap in:
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>
这篇关于如何在Angularjs控制器中使用初始谷歌地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文