前提・実現したいこと
緯度・経度が写らない。
Geocodeをgemに書いてbundle installしているのに…
発生している問題
Noodle.rbというモデルに記述しております。
Noodle.rb↓
class Noodle < ApplicationRecord belongs_to :user has_many :comments, dependent: :destroy mount_uploader :image, ImageUploader geocoded_by :address← after_validation :geocode← validates :shop_name, presence: true validates :noodle_name, presence: true validates :genre, presence: true validates :price, presence: true validates :address, presence: true validates :yummy, presence: true validates :instagram, presence: true validates :girl, presence: true validates :man, presence: true validates :famous, presence: true end
投稿一覧のviewはこのようになっております。↓
<div class="noodles-container"> <% @noodles.each do |t| %> <div class="noodle"> <div class="main-container"> <span> <h2>Shop name</h2> <h3><%= t.shop_name %></h3> </span> <div class="main-box"> <div class="top-container"> <%= image_tag t.image_url, size: "400x400" if t.image? %> </div> <div class="down-container"> <div class="right-container"> <b>Ramen name</b> <%= t.noodle_name %> <b>Genre</b> <%= t.genre %> <b>Price</b> <%= t.price %> <b>Address</b> <%= t.address %> <b>Latitude</b> <%= t.latitude %> <b>Longitude</b> <%= t.longitude %> </div> <div class="left-container"> <h1>Evaluation List!!</h1> <h2>Delicious! <span class="star-rating"> <span class="star-rating-front" style="width: <%= getPercent(t.yummy) %>%;">★★★★★</span> <span class="star-rating-back">★★★★★</span> </span> </h2> <h2>Instagrammable! <span class="star-rating"> <span class="star-rating-front" style="width: <%= getPercent(t.instagram) %>%;">★★★★★</span> <span class="star-rating-back">★★★★★</span> </span> </h2> <h2>Easy of entry for women! <span class="star-rating"> <span class="star-rating-front" style="width: <%= getPercent(t.girl) %>%;">★★★★★</span> <span class="star-rating-back">★★★★★</span> </span> </h2> <h2>Greasy! <span class="star-rating"> <span class="star-rating-front" style="width: <%= getPercent(t.man) %>%;">★★★★★</span> <span class="star-rating-back">★★★★★</span> </span> </h2> <h2>Little-known post! <span class="star-rating"> <span class="star-rating-front" style="width: <%= getPercent(t.girl) %>%;">★★★★★</span> <span class="star-rating-back">★★★★★</span> </span> </h2> </div> </div> <div class="index-link"> <div class="fb-share-button" data-href="http://localhost:3000/noodles" data-layout="button_count" data-size="small"><a target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Flocalhost%3A3000%2Fnoodles&src=sdkpreparse" class="fb-xfbml-parse-ignore">シェア</a></div> <%= link_to "Details☞", noodle_path(t.id) %> </div> </div> </div> <% end %> </div>
現在のjsコード
<div id='map'></div> <script> let map function initMap(){ geocoder = new google.maps.Geocoder() map = new google.maps.Map(document.getElementById('map'), { center: {lat: 35.68123620000001, lng:139.7671248}, zoom: 12, }); <% @noodles.each do |t| %> (function(){ var contentString = "住所:<%= t.address %>"; var marker = new google.maps.Marker({ position:{lat: <%= t.latitude %>, lng: <%= t.longitude %>}, map: map, title: contentString }); })(); <% end %> } </script> <script src="https://maps.googleapis.com/maps/api/js?key={}&callback=initMap" async defer></script>
投稿した後のjsコード
<script> let map function initMap(){ geocoder = new google.maps.Geocoder() map = new google.maps.Map(document.getElementById('map'), { center: {lat: 35.68123620000001, lng:139.7671248}, zoom: 12, }); (function(){ var contentString = "住所:東京都葛飾区青戸3丁目33−16"; var marker = new google.maps.Marker({ position:{lat: , lng: }, map: map, title: contentString }); })(); (function(){ var contentString = "住所:葛飾区"; var marker = new google.maps.Marker({ position:{lat: , lng: }, map: map, title: contentString }); })(); }
参考記事
【Rails6 / Google Map API】住所を投稿してマップに複数マーカーを立てる
補足情報(FW/ツールのバージョンなど)
windows 10 Home
rails 2.6.6