[英]create multiple google.maps.Map object with ng-repeat (angularJs)
I want to create multiple google maps objects inside every div id="googleMap", the problem is that only in the first rotation im getting google map object and in the rest rotations of my ng-repeat im getting blank-div (lets say red background as default) without the google map object inside. 我想在每个div id =“ googleMap”内创建多个Google Maps对象,问题是仅在第一个旋转中即时获取google map对象,而在其他ng-repeat即时旋转中获取blank-div(让我们说红色背景为默认值),而内部没有google map对象。
<div ng-repeat="gym in gyms track by $index ">
<div id="gymContainer">
<div id="googleMap"></div>
<div class="form-group gymDes">
Gym Name
<input ng-model="gym.name" type="text" class="form-control" readonly>
</div>
<div class="form-group gymDes">
City
<input ng-model="gym.city" type="text" class="form-control" readonly>
</div>
<div class="form-group gymDes">
Price
<input ng-model="gym.price" type="text" class="form-control" readonly>
</div>
<div id="gymLogo"></div>
<script>
function initMap() {
var mapDiv = document.getElementById('googleMap');
var map = new google.maps.Map(mapDiv, {
center: {lat: 44.540, lng: -78.546},
zoom: 8
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap"
async defer></script>
</div>
</div>
The ng-repeat will repeat the HTML specified within it, therefore, it will create several divs with the same id. ng-repeat将重复其中指定的HTML,因此,它将创建具有相同ID的多个div。 However, the name of the ids in the page must be unique. 但是,页面中ID的名称必须唯一。
Therefore, I would suggest to make those ids to change with each iteration. 因此,我建议使这些id随每次迭代更改。
You have to replace the id
property (that should be unique in a page) with a class
property. 您必须用class
属性替换id
属性(在页面中应该是唯一的)。 Then use getElementsByClassName
to initialize your map: 然后使用getElementsByClassName
初始化地图:
For your information, you will have to generate a unique id
/ class
per iteration such as <div class="map_container-{{$index}}">
and find a way to generate a custom init javascript function that you would pass in the google map callback (this way is not the right and angular way to achieve what you want): 供您参考,您必须在每次迭代中生成唯一的id
/ class
,例如<div class="map_container-{{$index}}">
并找到一种生成自定义init javascript函数的方法,该函数将在谷歌地图回调(这种方法不是实现您想要的正确和有角度的方法):
<div ng-repeat="gym in gyms track by $index ">
<div class="gymContainer">
<div class="map_container-{{$index}}"></div>
<!-- ... -->
<script>
function initMap() {
var mapDiv = document.getElementsByClassName('YOUR_map_container-with-index');
var map = new google.maps.Map(mapDiv, {
center: {lat: 44.540, lng: -78.546},
zoom: 8
});
}
</script>
</div>
</div>
The Angular way would be to create a custom directive, or maybe you should consider using the Angular Google Maps Angular的方法是创建自定义指令,或者您应该考虑使用Angular Google Maps
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.