我會用兩種寶石的幫助下處理這個將兩列添加到您的遊戲模型)。
class Game < ActiveRecord::Base
after_save :cache_coordinates
def cache_coordinates
loc = Geocoder.coordinates(address)
return if loc.nil?
update_column(:latitude, loc[0])
update_column(:longitude, loc[1])
end
end
,請務必遵守這裏列出來獲得Gmaps工作的指示: Google-Maps-for-Rails
您的地圖控制器可看起來像這樣(不要忘記添加路線...)
class MapsController < ApplicationController
respond_to :html, :js
def index
@geolocations = Game.all
@hash = Gmaps4rails.build_markers(@geolocations) do |geolocation, marker|
marker.lat geolocation.latitude
marker.lng geolocation.longitude
end
end
end
和一個簡單的看法是這樣的:
// use your API key here...
<%= javascript_include_tag "//maps.google.com/maps/api/js?v=3.23&sensor=false&libraries=geometry&key=YOURKEY" %>
<%= javascript_include_tag "//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js" %>
<script>
var mapStyle = [];
var mapOptions = {};
var handler = Gmaps.build('Google',
{markers:
{clusterer: {
gridSize: 20,
maxZoom: 13
}}});
handler.buildMap({
internal: {id: 'multi_markers'},
provider: {
scrollwheel: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
}, function(){
var markers = handler.addMarkers(<%=raw @hash.to_json %>);
// some options you may or may not want to use
handler.map.centerOn([40.397, -95.644]);
handler.fitMapToBounds();
handler.getMap().setZoom(4)
});
</script>