Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active April 21, 2016 03:17
Show Gist options
  • Save jacoyutorius/7472317 to your computer and use it in GitHub Desktop.
Save jacoyutorius/7472317 to your computer and use it in GitHub Desktop.
Rails4でGoogleMapを表示させる ref: http://qiita.com/jacoyutorius/items/a107ff6c93529b6b393e
<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
//= require underscore
//= require gmaps/google
rails new gmap
cd gmap
bundle install
rails g scaffold user title:string description:string address:string latitude:float longitude:float
rake db:migrate
rails g controller map index
gem "gmaps4rails"
gem "geocoder"
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @hash.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>
class MapController < ApplicationController
def index
@users = User.all
@hash = Gmaps4rails.build_markers(@users) do |user, marker|
marker.lat user.latitude
marker.lng user.longitude
marker.infowindow user.description
marker.json({title: user.title})
end
end
end
class User < ActiveRecord::Base
geocoded_by :address
after_validation :geocode
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment