简体   繁体   English

Link_to标签显示地图模态Rails 5

[英]Link_to tag to display map modal Rails 5

When I click on a map marker a modal pops up as such: 当我点击地图标记时,会弹出一个模态:

地图标记模态

I just implemented a search feature and am trying to get it when I click on a result a modal with all the information will pop up just like in the photo. 我刚刚实现了一个搜索功能,当我点击一个结果时,我试图得到它,所有信息的模态将像照片一样弹出。

搜索结果

Here is the code I am working with for that page: 这是我正在为该页面使用的代码:

MapsController: MapsController:

class MapsController < ApplicationController
  def index

      @maps = Map.all
      @hash = Gmaps4rails.build_markers(@maps) do |map, marker|
      marker.lat map.latitude
      marker.lng map.longitude
       marker.json({:id => map.id,
            :number => map.number,
            :name => map.name,
            :tabid => map.tabid,
            :zipcode => map.zipcode,
            :latitude => map.latitude,
            :longitude => map.longitude
                  })     

    end
  end

  def favorite 
    @map = Map.new(:number => 'Favorite Site')
    @map.save
    redirect_to :back
    flash[:success] = "favorited"
  end



  def show
   @maps = Map.find(params[:id])

end
end

SearchController: SearchController:

class SearchController < ApplicationController
  def index
    if params[:query].present?
     @maps = Map.search(params[:query]).with_pg_search_highlight
     @count = @maps.pluck(:id).count


     @hash = Gmaps4rails.build_markers(@maps) do |map, marker|
      marker.lat map.latitude
      marker.lng map.longitude
       marker.json({:id => map.id,
            :number => map.number,
            :name => map.name,
            :tabid => map.tabid,
            :zipcode => map.zipcode,
            :latitude => map.latitude,
            :longitude => map.longitude
                  })         



      # marker.infowindow render_to_string(:partial => "/maps/info", :locals => { :object => map})
       end
    else 
      @maps = Map.all
  end
end
end

Here is my code for my search result page: 这是我的搜索结果页面的代码:

<% provide(:page_title, 'Search Results') %>

<script src="//maps.google.com/maps/api/js?key=AIzaSyAxwNVY12NVNEbrnPorhkHRe7V0_xU8xHM&libraries=places"></script>

<% content_for :scripts %>
<%= javascript_include_tag 'creative/marker_cluster.js', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'creative/infobox_packed.js', 'data-turbolinks-track': 'reload' %>

<div class="main">

<div id="sideBar" class="pre-scrollable">
      <h2><%= @count%> results found </h2>
      <br>
  <% @maps.each do |map| %>
      <div>
        <div class="" id="map_<%= map.id %>">

        <h4>
          <%= link_to map.number, controller: "maps", action: "show", id: map.id %>
        </h4>


        <hr>

        </div>
      </div>
    <% end %>
    <%= link_to 'Return to Main Map', maps_path %>
 </div> 
 <div id="map_wrapper">
    <div id="map" style='width: 100%; height: 100%;'></div>
   </div>

</div>



<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
  <div class="modal-dialog">

          <!--Basic Table-->
          <div class="panel panel-green margin-bottom-40">
            <div class="panel-heading">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
              <div class = "name"></div>
            </div>
            <div class="panel-body">

                  <div id="map2" style='width: 568px; height: 365px;'>

                  </div>

                <div class="row">
    <div class="col-sm-12 text-center">



              </div>
              </div>
            </div>
            <table class="table paneltb">


            </table>

          </div>
          <!--End Basic Table-->
  </div>
</div>

 <script type = "text/javascript">
var handler = Gmaps.build('Google', {
         markers:
            {clusterer: {
              gridSize: 60,
              maxZoom: 20,
              styles: [ {
                textSize: 10,
                textColor: '#ff0000',
                url: 'assets/creative/m1.png',
                height: 60,
                width: 60 }
              , {
                textSize: 14, 
                textColor: '#ffff00',
                url:'assets/creative/m2.png',
                height: 60,
                width: 60 }
              , {
               textSize: 18, 
               textColor: '#0000ff',
               url: 'assets/creative/m3.png',
               width: 60,
               height: 60}
              ]}}
      });

var current;
function initialize(){
  handler.buildMap({ internal: {id: 'map'} }, function() {

    markers_json = <%=raw @hash.to_json %>;
    markers = _.map(markers_json, function(marker_json){
      marker = handler.addMarker(marker_json);
      handler.fitMapToBounds();
      _.extend(marker, marker_json);
      return marker;
    });

    getLocation();



    markers.map(function(elem, index) {

      google.maps.event.addListener(elem.getServiceObject(), "click", function(evt) {
        var id = elem.id,
            number = elem.number,
            name = elem.name,
            zipcode = elem.zipcode,
            tabid = elem.tabid,
            latitude = elem.latitude,
            longitude = elem.longitude



         $(".name").html("<h3 class='panel-title'><i class='fa fa-id-card'></i>"+number+"</h3>")
         $(".paneltb").html("<thead><tr><th>Panel</th><th>Location</th><th>Tab ID</th><th>Zip Code</th><th>Latitude</th><th>Longitude</th></tr></thead><tbody><tr><td>"+number+"</td><td>"+ name + "</td><td>"+tabid+"</td><td>"+zipcode+"</td><td>"+latitude+"</td><td>"+longitude+"</td></tr></tbody>")


        pos = new google.maps.LatLng( latitude, longitude );
        var div = document.getElementById('map2');
        var sv = new google.maps.StreetViewPanorama(div);



        sv.setPosition( pos );
        sv.setVisible( true );

        // find the heading by looking from the google car pos to the venue pos
        var service = new google.maps.StreetViewService();
        service.getPanoramaByLocation( pos, 50, function(result, status) {
            if (status == google.maps.StreetViewStatus.OK) 
            {   
                carPos = result.location.latLng;
                heading = google.maps.geometry.spherical.computeHeading( carPos, pos );
                sv.setPov( { heading: heading, pitch: 0, zoom: 0 } );
            }
        })

        $('#myModal').modal('show')

          current = elem;

      $("#myModal").on("shown.bs.modal", function () {
    google.maps.event.trigger(sv, "resize");
});  

        });









    })
  });
    // Create the search box and link it to the UI element.


}
function getLocation(){
  if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(displayOnMap);
  }
  else{
    navigator.geolocation.getCurrentPosition(displayOnMapError);
  }
};
function displayOnMap(position){

  marker2 = handler.addMarker({
    lat: position.coords.latitude,
    lng: position.coords.longitude,
    picture: {
        url: "<%= asset_path 'creative/1499326997_Untitled-2-01.png' %>",
        width:  48,
        height: 48
        },
    infowindow:  "You are Here!"
  });
  handler.map.centerOn(marker2);
  handler.getMap().setZoom(10);
};

function displayOnMapError(position){

  marker2 = handler.addMarker({
    lat: 34.0522,
    lng: -118.2437,
    picture: {
        url: "<%= asset_path 'creative/1499326997_Untitled-2-01.png' %>",
        width:  48,
        height: 48
        }
  });
  handler.map.centerOn(marker2);
  handler.getMap().setZoom(10);
};




initialize();


</script>

Right now when I click on a link I get redirected to the link with a map id which is nothing (see address) for example: 现在,当我点击一个链接时,我被重定向到一个地图ID没有的地址ID(见地址),例如:

重定向

If someone can guide me it would be greatly appreciated. 如果有人可以指导我,我将不胜感激。

Update 1 更新1

When I use the suggested answer I was given I do get a modal pop-up. 当我使用建议的答案时,我得到了一个模态弹出窗口。 The goal is to get the modal to give me the same information as if I clicked on a map marker (see first image). 目标是让模态给我相同的信息,就像我点击地图标记一样(见第一张图片)。 When I initially click on the search result number I get the following: 当我最初点击搜索结果编号时,我得到以下内容:

空白模态

However, when I click on a map marker and its modal pops up when I click on the results links each modal that pops up will show the information for the last map marker modal that popped up, meaning the modal looks correct as in the first image its just it is the same modal for each link in the search results. 但是,当我单击地图标记并且当我点击结果链接时弹出其模态时,弹出的每个模态将显示弹出的最后一个地图标记模态的信息,这意味着模态看起来与第一个图像中的相同它只是搜索结果中每个链接的模态。

Update 2 更新2

I think what my problem is, is that those links are not map markers thus the javascript written does not apply to them. 我认为我的问题是,这些链接不是地图标记,因此编写的javascript不适用于它们。 How can I have the links act the same as the map markers so the information gets transferred over to the modal. 如何使链接的行为与地图标记相同,以便将信息转移到模态。

Update 3 更新3

I am trying as suggested to pull the function out and onclick to call it so far I have is this and its not working or me. 我正在尝试将该功能拉出来并onclick来调用它到目前为止我有这个它不起作用或我。

<%= link_to map.number, "#", data: {toggle: "modal", target: "#myModal"}, :html => {:onclick => 'initialize()' } %>

Currently you have your link like this: 目前,您的链接如下:

<%= link_to map.number, controller: "maps", action: "show", id: map.id %>

That's creating basic links that go to your /maps/{id} url. 这是创建到/ maps / {id}网址的基本链接。

To open a bootstrap modal with a link, do: 要使用链接打开引导模式,请执行以下操作:

<%= link_to map.number, "#", data: {toggle: "modal", target: "#myModal"} %>


Update: 更新:

First, your initialize function already displays the modal with $('#myModal').modal('show') so you can get rid of the data: {toggle: "modal", target: "#myModal"} from the links. 首先,你的initialize函数已经显示了带有$('#myModal').modal('show')所以你可以从链接中删除data: {toggle: "modal", target: "#myModal"}

Next, your initialize function needs the specific map marker element passed into it, so I suggest doing this: 接下来,你的initialize函数需要传递给它的特定map标记元素,所以我建议这样做:

<%= link_to map.number, "#", class: "map-marker-link", data: {elem: map} %>

Then in your javascript: 然后在你的JavaScript中:

$(".map-marker-link").click(function (event) {
    initialize($(this).data('elem'));
    event.preventDefault();
});

If I got your question then the point is, you need to show map modal after clicking on the link right? 如果我得到你的问题然后重点是,你需要在点击链接后显示地图模态吗?

If yes then follow the steps 如果是,则按照步骤操作

  1. Create a partial modal to any view folder better is in layout folder _modal.html.erb 在布局文件夹_modal.html.erb更好地为任何视图文件夹创建部分模态

     <div class="modal fade my-modal" id="modalOne" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span><span class="sr-only">Close</span> </button> <h4 class="modal-title" id="myModalLabel">Map</h4> </div> <div class="modal-body"></div> </div> </div> </div> 
  2. Inside your maps folder create partial _show.html.erb 在您的地图文件夹中创建部分_show.html.erb

     <div id="map_wrapper"> <div id="map" style='width: 100%; height: 100%;'></div> </div> #=> with js script 
  3. Inside your maps folder create partial show.js.erb 在您的地图文件夹中创建部分show.js.erb

     $modal = $('.modal'), $modalBody = $('.modal .modal-body'), $modalHeading = $('.modal .modal-title'); $modalHeading.html("Map"); $modalBody.html('<%= escape_javascript(render("show")) %>'); $modal.modal(); 
  4. Render the modal partial on your result page below 在下面的结果页面上渲染模态部分

     <%= render partial: "../path/modal" %> 
  5. Create a link with remote true 创建一个远程true的链接

     <%= link_to map.number, map_path(map_parameter1, map_parameter2), remote: true, id: map.id %> #=> pass your map parameter with link to the show modal 

Note: JS script you can able to use _show.html.erb or show.js.erb 注意:JS脚本可以使用_show.html.erbshow.js.erb

Very carefully implement this 非常仔细地实现这个

Hope to help! 希望能帮到你!

I usually handle these using server-side Javascript. 我通常使用服务器端Javascript处理这些。 Try this out: 试试这个:

  1. Add a data-remote="true" to your link: 在您的链接中添加data-remote =“true”:

     <%= link_to map.number, controller: "maps", action: "show", id: map.id, data: { remote: true } %> 
  2. On your controller, add a JS response 在您的控制器上,添加一个JS响应

     def show @maps = Map.find(params[:id]) respond_to :js end 
  3. Create a new view file to be rendered. 创建要呈现的新视图文件。 Should be called 'show.js.erb' and be located in the 'app/views/maps/' directory 应该叫做'show.js.erb'并位于'app / views / maps /'目录中

     // Javascript code. // Whatever you write here will be executed alert("You just clicked on Map <%= @maps.id %>"); 

The javascript that you write on the view file should be written out to populate the modal and show it. 您应该写出您在视图文件上编写的javascript以填充模式并显示它。 You'll be able to copy much of the JS code that you posted in your original question. 您将能够复制您在原始问题中发布的大部分JS代码。

I have faced the same issue with map on modal, 我在面对模态时遇到了同样的问题,

Your map already there but you have to trigger resize the map when you open a modal like below: 您的地图已经存在,但是当您打开如下所示的模态时,您必须触发调整地图的大小:

google.maps.event.trigger(map, "resize")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM