0
我想从数据库中插入纬度和经度值到我的谷歌地图咖啡文字。js.coffee.erb - 同步问题?
jQuery ->
initialize()
initialize = ->
myOptions =
center: new google.maps.LatLng(<%= @location.latitude %>, <%= @location.longitude %>)
zoom: 12
mapTypeControlOptions: {mapTypeIds: ["OSM", "OCM", "MQ", google.maps.MapTypeId.HYBRID]}
map = new google.maps.Map $('#map_canvas')[0], myOptions
[...]
我收到以下错误信息:
undefined method `latitude' for nil:NilClass
(in /Users/sg/rails-projects/geo_rails_test/app/assets/javascripts/gmap.js.coffee.erb)
表明我的位置对象
@location = Location.find(params[:id])
尚未在当时实例解析js.coffee.erb文件。 (??)
我已经用硬编码值测试了咖啡脚本,@ location.latitude完美地工作在我看来。 任何想法发生了什么问题?
是的,我曾经想过这样的事情。感谢您的信息,我想长期的AJAX是更优雅的解决方案。 – donsteffenski
我会把'数据坐标= “{纬度:<%= @ location.latitude%>,LNG:<%= @ location.longitude%>]”'上#map_canvas与'$拿回来('# map_canvas')。data('coordinates')' – Guillaume86
@ Guillaume86:这也是一个不错的选择,或者根据具体情况分开'data-lat'和'data-lng'属性。 –