0.01
No commit activity in last 3 years
No release in over 3 years
gmaps-rails is a gem which includes GMaps.js and provides an easy way to use it in Rails. GMaps.js has over 4,000 stars on Github and you will love to use it with Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.6
>= 0

Runtime

>= 0
 Project Readme

gmaps-rails

gmaps-rails is a gem which includes GMaps.js and provides an easy way to use it in Rails.

GMaps.js has over 4,000 stars on Github and you will love to use it with Rails.

  • This is not gmaps4rails gem.

  • If you just want GMaps.js in your Rails app, I recommend gmapsjs.

Installation

Add this line to your application's Gemfile:

gem 'gmaps-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install gmaps-rails

Usage

GMaps.js

First of all, you have to include Google Maps API in a view file or in a layout file.

<%= javascript_include_tag 'http://maps.googleapis.com/maps/api/js?key=YOUR_KEY&sensor=true' %>

You can use GMaps.js simply by requiring gmaps in app/assets/javascripts/application.js file.

    :
    :
//= turbolinks
//= gmaps
//= require_tree .

Helpers

GMaps in Ruby

In addition to that, this gem has helpers.

You can define GMaps object in a controller or presenter (such as Draper).

@map = GMaps.new(div: '#map', lat: -12.043333, lng: -77.028333)
@map.addMarker(lat: -12.043333,
               lng: -77.028333,
               title: 'Lima',
               click: GMaps::JS["function(e) { alert('You clicked in this marker'); }"])
@map.addMarker(lat: -12.042,
               lng: -77.028333,
               title: 'Marker with InfoWindow',
               infoWindow: {
                 content: '<p>HTML Content</p>'
               })

You can show maps as follows:

<script>
  jQuery(function() {
    <%= @map.to_js %>
  })
</script>
<body>
  <div sytle='width: 800px'>
    <div id='map' style='width: 800px; height: 400px'></div>
  </div>
</body>

gmaps_ujs

By requiring gmaps_ujs in addition to gmaps, you can remove all JavaScript code for Google Maps.

    :
    :
//= turbolinks
//= gmaps
//= gmaps_ujs
//= require_tree .

Controller code is same as the above example, but you can omit div parameter when initializing GMaps object.

@map = GMaps.new(lat: -12.043333, lng: -77.028333)

In a view file, you can show maps by using map_div helper method.

<body>
  <div sytle='width: 800px'>
    <%= map_div @map, style: 'width: 800px; height: 400px' %>
  </div>
</body>

Contributing

  1. Fork it ( https://github.com/kentaroi/gmaps-rails/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request