gpt4 book ai didi

javascript - 谷歌地图不显示模型 Rails 5 中的标记

转载 作者:数据小太阳 更新时间:2023-10-29 08:58:15 25 4
gpt4 key购买 nike

我正在 Rails 5、Ruby 2.4.0 上构建应用程序并尝试使用 Google Maps API V3。

就是说,我的位置模型中有两个位置,我想将它们显示在我的 locations/index.html.erb 上显示的 map 上。

我关注了这个stack question希望我可以修改它以满足我的需要,但无济于事。 map 会自行加载,但不会出现任何标记。

我的api链接在我的head中的application/layout.html.erb中,在我的locations/index.html.erb文件中找到如下内容:

<script type="text/javascript">
var locations = [
<% @locations.each do |location| %>
{ lat:<%= location.latitude %>, lng:<%= location.longitude %> },
<% end %>
];

var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;

for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});

google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>

本质上,我试图遍历每个位置并将每个位置的纬度和经度提供到新标记中并显示在索引页面 map 上。

这里的任何帮助将不胜感激,因为我现在真的被困在这里了!

谢谢。

EDIT #1

enter image description here

EDIT # 2 enter image description here

EDIT # 3 enter image description here

最佳答案

您需要从回调中添加 init 函数,否则您的代码会在加载所有 Google Maps JS 库之前执行。

https://developers.google.com/maps/documentation/javascript/tutorial

在你的情况下:

...
function initMap(){
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
.....

关于javascript - 谷歌地图不显示模型 Rails 5 中的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com