我正在让 gmaps4rails 处理我的项目,但我正在努力自定义信息窗口的方面。我遵循了 wiki 中的教程,除了 InfoBox 的回调部分之外,我理解了其中的大部分内容,这对我这个没有经验的 JS 程序员来说有点令人困惑。当我单击标记时,我只看到“x”(关闭)按钮,但没有显示任何文本或颜色。这是我的代码:
in Post.rb:
def gmaps4rails_infowindow
# add here whatever html content you desire, it will be displayed when users clicks on the marker
"<h4>#{self.title}</h4>"
end
在 posts.js.coffee 中:
Gmaps.map.infobox = (boxText) ->
content: boxText
disableAutoPan: false
maxWidth: 0
pixelOffset: new google.maps.Size(-140, 0)
zIndex: null
boxStyle:
background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/examples/tipbox.gif') no-repeat"
opacity: 0.75
width: "280px"
closeBoxMargin: "10px 2px 2px 2px"
closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
infoBoxClearance: new google.maps.Size(1, 1)
isHidden: false
pane: "floatPane"
enableEventPropagation: false
在 gmaps4rails.css 中
.yellow { border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px; }
在我看来:
= gmaps("markers" => {"data" => @json, "options" => {"custom_infowindow_class" => "yellow" } })
如果有人能为我指出正确的方向,我将非常感激,因为这是我第一次使用 map 。预先感谢您!
编辑:
我的结果:
http://postimage.org/image/45feoz3kl/
编辑2:
我不想虐待你,但一旦我明白发生了什么事,我会自己做好事,目前情况是最糟糕的。现在它甚至无法渲染,而且我收到了一堆错误。这是我的代码:
= gmaps("markers" => {"data" => @json, "options" => {"custom_infowindow_class" => "yellow" } })
- content_for :scripts
:javascript
Gmaps.map.infobox = function(boxText) {
return {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: null
,boxStyle: {
background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.5/examples/tipbox.gif') no-repeat"
,opacity: 0.75
,width: "280px"
}
,closeBoxMargin: "10px 2px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
}};
错误:
返回语句行中出现“意外的keyword_ensure,期望$end”。
最佳答案
经过一些评论,我终于明白了(即使它毕竟是显而易见的......):
Gmaps.map
是在页面加载时动态创建的。因此,每当您想要向此对象添加属性时,都必须将其添加到 content_for :scripts< 中的
.gmaps
调用and之后/
在您的代码中:
= gmaps()
- content_for :scripts do
:javascript
Gmaps.map.infobox = function(....
关于ruby-on-rails - Gmaps4Rails 自定义信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10803209/