Home>

I'm new to programming.
I want to get the latitude and longitude from the registered address and display it on google map.
I can't get the latitude and longitude well, so can you tell me if there is a cause you can think of?

The following is already set
・ Create a project on google cloud platform and register payment information
-Obtain the API key and describe it in the .env file
· Enable Geocoding API, Maps JavaScript and Places API API
-API key authentication information has no application restrictions and 3 API restrictions (Geocoding API, Maps JavaScript API, Places API)
-Looking at the API history (?), Request 14 and error 1 are displayed, so it seems that communication is possible.

The latitude and longitude cannot be obtained from the address entered on the registration form, resulting in an error and the content entered on the registration form cannot be registered. (Because the validation of latitude and longitude is presence: true)
Also, if you remove presence: true from the validation, it will be registered as null, so you cannot display the google map.

Error message
Google API error: request denied (You must use an API key to authenticate each request to Google Maps Platform APIs.

Corresponding source code
<schema.rb>create_table "tours", options: "ENGINE = InnoDB DEFAULT CHARSET = utf8", force :: cascade do | t |
    t.integer "user_id"
    t.text "title"
    t.datetime "date"
    t.string "image"
    t.text "detail"
    t.float "latitude"
    t.float "longitude"
    t.string "address"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
<tour.rb>Calculate latitude and longitude based on geocoded_by: address #address column
 after_validation: geocode, if :: address_changed? #Change latitude and longitude when changing address
<tours.controller>class ToursController ?", Date.today) .paginate (page: params [: page],

 per_page: 10)
  enddef new
    @tour = Tour.new
  end
  def create
    @tour = Tour.new (tour_params)
    @ tour.user_id = current_user.id
    if @ tour.save
      redirect_to tour_path (@ tour.id)
    else else
      render: new
    end
  end
  def show
    @tour = Tour.find (params [: id])
  end
  def edit
    @tour = Tour.find (params [: id])
  end
  def update
    @tour = Tour.find (params [: id])
    if @ tour.update! (tour_params)
      flash [: success] = "The tour content has changed"
      redirect_to tour_path (@ tour.id)
    else else
      render: edit
    end
  end
  def destroy
    @tour = Tour.find (params [: id]). delete
    redirect_to user_path (current_user.id), info:'Deleted tour'
  end
  private
    def tour_params
      params.require (: tour) .permit (: user_id,: title,: date,: image,: detail,: latitude,: longitude,: address)
    end
end
<new.hml.erb>               Creating a tour to be held       <% = form_for @tour, url: tours_path do | f |%>      <% = render'layouts/error_messages', model: f.object%>                  <% = f.label: title, "Tour name"%>        <% = f.text_field: title, class:'tour-input-form form-control'%>                            <% = f.label: date, "Tour date and time"%>        <% = f.datetime_field: date, class: "datetimepicker-input form-control"%>                            <% = f.label: detail, "Tour details"%>        <% = f.text_field: detail, class:'tour-input-form form-control'%>                            <% = f.label: address, "Tour meeting place"%>        <% = f.text_field: address, class:'tour-input-form form-control'%>                            <% = f.submit "submit"%>              <% end%>        
<Gemfile>gem'geocoder' #google map Calculate latitude and longitude from address
gem'gon' # Make instance variables defined in the controller available in the view's JavaScript
What I tried

-Reissuing the API key and rewriting the env file accordingly
・ I tried to register the address as "Tokyo" or "1-chome Marunouchi, Chiyoda-ku, Tokyo" from a large area to a detailed address, but I could not get the latitude and longitude.

Supplementary information (FW/tool version, etc.)

rails 5.2.4
Development environment AWS cloud9