我正在尝试将折线与 gmaps4rails 2 一起使用,但无法弄清楚错误是什么......
当我对折线进行硬编码时,地图会按预期显示。但是,当我从控制器获取数据时,我得到“未捕获的类型错误:数字不是函数”
以下带有硬编码折线的代码有效
看法
<script>
$('#myModal2').on('shown.bs.modal', function (e) {
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map2'}}, function(){
polyline = [{"lat":49.9574400,"lng":-123.1201800}, {"lat":49.9465300,"lng":-123.0553700},{"lat":49.9598300,"lng":-123.0475400},{"lat":49.9750500,"lng":-123.0427700}];
handler.addPolyline(polyline);
handler.bounds.extend(polyline[0]);
handler.bounds.extend(polyline[ polyline.length - 1]);
handler.fitMapToBounds();
handler.getMap().setZoom(12);
});
});
</script>
但是这个没有
<script>
$('#myModal2').on('shown.bs.modal', function (e) {
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map2'}}, function(){
polyline = <%=raw @hashroute.to_json %>;
handler.addPolyline(polyline);
handler.bounds.extend(polyline[0]);
handler.bounds.extend(polyline[ polyline.length - 1]);
handler.fitMapToBounds();
handler.getMap().setZoom(12);
});
});
</script>
控制器
def show
@hashroute =[]
@list.routes.each do |route|
@hashroute << { :lat => route.from_lat, :long => route.from_long}
@hashroute << { :lat => route.to_lat, :long => route.to_long}
end
respond_to do |format|
format.html {render :show}
format.json { head :ok}
end
end
@hashroute.to_json 已正确填充,控制台中的折线看起来与硬编码版本相同
有效的控制台代码
当它不起作用时
任何的想法?