質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

1601閲覧

railsで住所の位置をgoogle mapに表示させたいのですが、google mapが表示されません。

RyotaSaito

総合スコア7

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/06/24 09:12

前提・実現したいこと

Railsにて、Googe Map API並びにgemのgeocoderを用いて、
会員登録時に住所・緯度・経度もデータベースに保存し、マイページでGoogle Map上に住所の位置を表示させたいです。
下記記事を参考に作成しました。
https://qiita.com/93pMoB8Pi0aFXMG/items/629a7f78e118e3bac1bc

発生している問題・エラーメッセージ

Google Mapが表示されず、binding.pryをしてみると保存時にlatitude, longitudeのカラムがnilになっています。

また検証ツール上にもエラーが出ておりますが、解決策が分かりかねております。

大変お手数ですが、指南いただけると幸いです。

検証ツールエラー1 Uncaught SyntaxError: Unexpected token ',' 検証ツールエラー2 7:1 Uncaught (in promise) Vc message: "initMap is not a function"

該当のソースコード

html.erb

1User/showページ 2 3<% if @user == current_user %> 4 5<script type="text/javascript"> 6 function initMap() { 7 8 var test ={lat: <%= @user.latitude %>, lng: <%= @user.longitude %>}; 9 var map = new google.maps.Map(document.getElementById('map'), { 10 zoom: 15, 11 center: test 12 }); 13 var transitLayer = new google.maps.TransitLayer(); 14 transitLayer.setMap(map); 15 16 var contentString = '住所:<%= @user.address %>'; 17 var infowindow = new google.maps.InfoWindow({ 18 content: contentString 19 }); 20 21 var marker = new google.maps.Marker({ 22 position:test, 23 map: map, 24 title: contentString 25 }); 26 27 marker.addListener('click', function() { 28 infowindow.open(map, marker); 29 }); 30 } 31</script> 32 33<script async defer 34 src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=<%= ENV['GOOGLE_MAP_API_KEY'] %>&callback=initMap"> 35</script> 36 37<style type="text/css"> 38 #map { height: 200px; 39 width: 70%;} 40</style> 41 42<div id="map"></div> 43 44<% end %>
ユーザーコントローラ class UsersController < ApplicationController before_action :authenticate_user! before_action :baria_user, only: [:update] def show @user = User.find(params[:id]) @books = @user.books @book = Book.new #new bookの新規投稿で必要(保存処理はbookコントローラー側で実施) end def edit @user = User.find(params[:id]) if @user != current_user redirect_to user_path(current_user) end end def update @user = User.find(params[:id]) if @user.update(user_params) redirect_to @user, notice: "successfully updated user!" #user_path(@user)の略。updateしたばかりの@userを意味する。 else render :edit end end private def user_params params.require(:user).permit(:name, :introduction, :profile_image, :postal_code, :prefecture, :address) end
userモデル geocoded_by :address after_validation :geocode, if: :address_changed?
Geocoder.configure( # Geocoding options # timeout: 3, # geocoding service timeout (secs) lookup: :google, # name of geocoding service (symbol) # ip_lookup: :ipinfo_io, # name of IP address geocoding service (symbol) # language: :en, # ISO-639 language code use_https: true, # use HTTPS for lookup requests? (if supported) # http_proxy: nil, # HTTP proxy server (user:pass@host:port) # https_proxy: nil, # HTTPS proxy server (user:pass@host:port) #YOUR_API_KEYにはご自身のAPIキーを記述してください。 api_key: ENV['GOOGLE_MAP_API_KEY'], # API key for geocoding service # cache: nil, # cache object (must respond to #[], #[]=, and #del) # cache_prefix: 'geocoder:', # prefix (string) to use for all cache keys # Exceptions that should not be rescued by default # (if you want to implement custom error handling); # supports SocketError and Timeout::Error # always_raise: [], # Calculation options # units: :mi, # :km for kilometers or :mi for miles # distances: :linear # :spherical or :linear )

schema

1 2 create_table "users", force: :cascade do |t| 3 t.string "email", default: "", null: false 4 t.string "encrypted_password", default: "", null: false 5 t.string "reset_password_token" 6 t.datetime "reset_password_sent_at" 7 t.datetime "remember_created_at" 8 t.integer "sign_in_count", default: 0, null: false 9 t.datetime "current_sign_in_at" 10 t.datetime "last_sign_in_at" 11 t.string "current_sign_in_ip" 12 t.string "last_sign_in_ip" 13 t.string "name" 14 t.datetime "created_at", null: false 15 t.datetime "updated_at", null: false 16 t.text "introduction" 17 t.string "profile_image_id" 18 t.string "prefecture_code" 19 t.string "postal_code" 20 t.string "address" 21 t.float "latitude" 22 t.float "longitude" 23 t.index ["email"], name: "index_users_on_email", unique: true 24 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 25 end

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問