表題の通りです。
Map表示のために、APIが必要とのことなのでkeyを取得し読み込んでいます。
- Viewの部分
html
1 2<!-- determining js url for googlemap --> 3<script type="text/javascript" src="//maps.google.com/maps/api/js?key=[myAPIKey]"></script> 4 5<h1>Map#index</h1> 6<p>Find me in app/views/map/index.html.erb</p> 7 8<div style='width: 800px;'> 9 <div id="map"></div> 10</div> 11 12 13 14<script type="text/javascript"> 15 handler = Gmaps.build('Google'); 16 handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 17 markers = handler.addMarkers(<%=raw @hash.to_json %>); 18 handler.bounds.extendWith(markers); 19 handler.fitMapToBounds(); 20 }); 21</script>
- railsのapplication.jsにてunderscore.jsとmakerclusterer.jsを読み込んでいます
- 表示するためのcontrollerとmodel(model名はlocations)
ruby:/app/controller/map_controller.rb
1def index 2 @locations = Location.all 3 @hash = Gmaps4rails.build_markers(@locations) do |location, marker| 4 marker.lat location.latitude 5 marker.lng location.longitude 6 marker.infowindow location.description 7 marker.json({title: location.title}) 8 end 9 end
ruby:/app/models/location.rb
1class Location < ApplicationRecord 2 # for googlemap 3 geocoded_by :address 4 after_validation :geocode 5end
環境
- rails (5.0.0)
- gem gmaps4rails (2.1.2)
- gem geocoder (1.4.1)
何か見落としているとこありましたら、お願いします。。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。