Google Maps API使用Rails 4中的Gmaps4rails Gem查看并填充问题

Google Maps API View and populate issues with Gmaps4rails Gem in Rails 4

本文关键字:Gem 问题 填充 Gmaps4rails 中的 API Maps 使用 Rails Google      更新时间:2023-09-26

我正试图按照本教程在这里为我的应用程序添加一个新的脚手架。在视频中,模型名为User,但我的模型名为Reports。我已经到了4:14,但地图加载不正确;请参阅此处查看屏幕截图。它不会放大,只显示蓝色。当我以这样或那样的方式拖动它时,它会变成灰色。

在这一点上,我准确地遵循了教程,但我想知道脚手架是否与我的其他模型不兼容?或者,是引导CSS模板扰乱了这个地图的视图。

问题有两个——视图加载不正确,报告没有在地图上填充。我已经在下面粘贴了一些源代码,如果你想看其他文件,请告诉我,非常感谢你的帮助!

**reports_controller.rb**
class ReportsController < ApplicationController
  before_action :set_report, only: [:show, :edit, :update, :destroy]
  # GET /reports
  # GET /reports.json
  def index
    @reports = Report.all
    @hash = Gmaps4rails.build_markers(@reports) do |report, marker|
      marker.lat report.latitude
      marker.lng report.longitude
      marker.infowindow report.description
    end
  end
  # GET /reports/1
  # GET /reports/1.json
  def show
  end
  # GET /reports/new
  def new
    @report = Report.new
  end
  # GET /reports/1/edit
  def edit
  end
  # POST /reports
  # POST /reports.json
  def create
    @report = Report.new(report_params)
    respond_to do |format|
      if @report.save
        format.html { redirect_to @report, notice: 'Report was successfully created.' }
        format.json { render action: 'show', status: :created, location: @report }
      else
        format.html { render action: 'new' }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end
  # PATCH/PUT /reports/1
  # PATCH/PUT /reports/1.json
  def update
    respond_to do |format|
      if @report.update(report_params)
        format.html { redirect_to @report, notice: 'Report was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @report.errors, status: :unprocessable_entity }
      end
    end
  end
  # DELETE /reports/1
  # DELETE /reports/1.json
  def destroy
    @report.destroy
    respond_to do |format|
      format.html { redirect_to reports_url }
      format.json { head :no_content }
    end
  end
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_report
      @report = Report.find(params[:id])
    end
    # Never trust parameters from the scary internet, only allow the white list through.
    def report_params
      params.require(:report).permit(:latitude, :longitude, :address, :description, :photo, :title, :text)
    end
end

-*-

**index.html.erb**
<script src="//maps.google.com/maps/api/js?v=3.13&amp;sensor=false&amp;libraries=geometry" type="text/javascript"></script>
<script src='//google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js' type='text/javascript'></script>
<h1>Listing reports</h1>
<table>
  <thead>
    <tr>
      <th>Latitude</th>
      <th>Longitude</th>
      <th>Address</th>
      <th>Description</th>
      <th>Photo</th>
      <th>Title</th>
      <th>Text</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% @reports.each do |report| %>
      <tr>
        <td><%= report.latitude %></td>
        <td><%= report.longitude %></td>
        <td><%= report.address %></td>
        <td><%= report.description %></td>
        <td><%= report.photo %></td>
        <td><%= report.title %></td>
        <td><%= report.text %></td>
        <td><%= link_to 'Show', report %></td>
        <td><%= link_to 'Edit', edit_report_path(report) %></td>
        <td><%= link_to 'Destroy', report, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>
<br>
<%= link_to 'New Report', new_report_path %>
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers(<%=raw @hash.to_json %>);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});
</script>

是的,这很可能是一个css问题,对gmaps4rails没有什么特别的,只是引导程序和谷歌地图之间已知的不兼容,请参阅gem-doc

您需要添加:

#map img { 
  max-width: none;
}
#map label { 
  width: auto; display:inline; 
}