我有一个使用谷歌地图API的应用程序。我正试图在地图上显示简单的标记。标记是我的数据库中的项目(包括纬度和经度值)。我可以在地图上显示一个标记,但无法用数据库中的所有项目填充地图。
我只能对每个项目调用数据库。任何建议都将不胜感激。
我的助手文件:
def name(x)
@name = Place.find(x).name
return @name
end
def latitude(x)
@latitude = Place.find(x).latitude
return @latitude
end
def longitude(x)
@longitude = Place.find(x).longitude
return @longitude
end
这是我为地图创建的变量:
var myLatlng = new google.maps.LatLng(<%= latitude(1) %>, <%= longitude(1)%>)
var marker1 = new google.maps.Marker({
position: myLatlng,
map: map,
title: "<%= name(1) %>"
});
这是html.haml语法——在布局上初始化一个js数组,如下
window.markers = [];
然后在您的查看页面上-
@places = Place.all
@places.each do |place|
#javascript code
$(document).ready(function(){
window.markers.push([ new google.maps.LatLng(#{place.latitude},#
{place.longitude}), "#{place.name}"]);
});
end
js代码在视图中
$.each(window.markers,function(i,e){
var marker = new google.maps.Marker({
position: e[0],
title: e[1],
map: map
});
});