1
新手入门,尝试使用Google Maps API。我正在寻找循环并显示数据库中每个地理位置的标记。当我在窗口中打开控制台并查找变量[places](应具有所有我的@locations
的值)时,我收到上述错误消息。请帮忙吗?谢谢。错误消息:“#<位置:: ActiveRecord_Relation:0x007f8d79d3de30 >”
map.html.erb:(
<h1>map Page</h1>
map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(37.725685, -122.156830),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var places = '<%= @locations %>';
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < places.length; i++) {
var places = '<%= @locations %>';
marker = new google.maps.Marker({
position: new google.maps.LatLng(places[i].lat, places[i].lng),
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(places[i].description);
infowindow.open(map, marker);
}
})(marker, i));
}
位置控制器:
个def map
@locations = Location.all
end
位置型号:
class CreateLocations < ActiveRecord::Migration[5.0]
def change
create_table :locations do |t|
t.string :location_name
t.string :location_address
t.string :location_description
t.float :lat
t.float :lng
t.timestamps
end
end
end
路线:
get '/locations/map', to: "locations#map", as: "map"
刚注意到,我删除了第二个 var places ='<%= @locations%>'; 从循环内部。虽然同样的问题。 – ZeroCalm
您好,请添加完整的错误信息 – Bohdan
“#<位置:: ActiveRecord_Relation:0x007f8d7939e758 >” 是我得到的唯一错误,并且它只在键入“places”时显示在控制台中。没有其他错误。 – ZeroCalm