开发者

Passing paramters from rails views to jquery (gmap)

I want to use this jquery plugin in my rails3 apps http://gmap.nurtext.de/ (Google Maps Plugin for jQuery)

It seems very easy to use:

$("#map").gMap({ markers: [{ latitude: 47.660937,
                              longitude: 9.569803,
                              zoom: 6 });

But i have a question, how i pass the values to this jasvascript code?

something like <%= show_map ("47.66","9.56","6") %>

开发者_如何学Go

or more harcoded

$("#map").gMap({ markers: [{ latitude: variable1,
                              longitude: variable2,
                              zoom: variable3 });


You need to create a helper function that returns a string containing the map JavaScript using the parameters you specify.

In your app/helpers/controllername_helper.rb file:

def show_map(lat, long, zoom)
  "$('#map').gMap({ markers: [{ latitude: #{lat}, longitude: #{long}, zoom: #{zoom} });"
end

Then in your view app/views/controllername/actionname, call it using the same code you referenced in your question:

<script type="text/javascript">
  <%= show_map ("47.66","9.56","6") %>
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜