简体   繁体   English

多个Google地图预览,用于多个自动完成字段

[英]multiple google map preview for multiple autocomplete fields

I have used google maps api autocomplete to search for a shop and display its city and country on the html form. 我已经使用Google Maps API自动完成功能搜索商店并在html表单上显示其城市和国家。 I also update a map preview for the searched shop. 我还为搜索到的商店更新了地图预览。 This is working fine until now. 到目前为止,一切正常。

I have added a possibility to duplicate the process: adding another shop, removing one, and marking one as the "first" shop. 我增加了重复该过程的可能性:添加另一家商店,删除一家,并将其中一家标记为“第一家”商店。 This makes the same fields be repeated anew for the new shop. 这使得对于新商店,相同的字段将被重新重复。 Thus autocomplete field and map preview also get duplicated. 因此,自动完成字段和地图预览也将重复。 Fine. 精细。

But when I add a shop, the map preview is not working for the second, third, ... ones. 但是,当我添加一家商店时,第二个,第三个……的地图预览不起作用。

Question: 题:

How can I fix this up so that the map preview gets updated for each shop separately? 如何解决此问题,以便分别为每个商店更新地图预览?

here is my jsfiddle: LINK. 这是我的jsfiddle:链接。

I am tracking my shops by $index : 我通过$index跟踪我的商店:

<fieldset ng-repeat="shop in myModel.shops track by $index">

I think I need to do the same indexing for map as well, but things get complicated and can't properly do it. 我认为我也需要对地图进行相同的索引编制,但是事情变得复杂且无法正确执行。

The whole code is in angularjs, by the way. 顺便说一下,整个代码都在angularjs中。

update: 更新:

taking into account AHC's answer, both maps seem to get updated, but the maps are empty on mine and are greyed out. 考虑到AHC的回答,这两个地图似乎都已更新,但是我的地图是空的并且显示为灰色。 Does anybody know the reason? 有人知道原因吗?

you just need to add the index to the input id and map preview id tags. 您只需要将索引添加到输入ID和地图预览ID标签即可。 and refer to them also by index in the function. 并通过函数中的索引对其进行引用。 Here is the jsfiddle . 这是jsfiddle I hope this works for you. 希望这对您有用。

id="pac-input-{{$index}}"
...
id="map_canvas-{{$index}}"

and inside the function: 在函数内部:

  var inputID = "pac-input-"+index;
  var input = document.getElementById(inputID);

  var mapID = "map_canvas-"+index;
  map = new google.maps.Map(document.getElementById(mapID), mapOptions);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM