gpt4 book ai didi

javascript - Link_to 标签显示 map 模态 Rails 5

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:44 25 4
gpt4 key购买 nike

当我点击 map 标记时,会弹出一个模式:

map marker modal

我刚刚实现了一个搜索功能,并试图在我点击一个结果时获得它,一个包含所有信息的模式将弹出,就像照片中一样。

search results

这是我为该页面使用的代码:

map Controller :

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

搜索 Controller :

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

这是我的搜索结果页面代码:

<% 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>

现在,当我点击一个链接时,我会被重定向到 map ID 为空的链接(参见地址),例如:

redirection

如果有人能指导我,将不胜感激。

更新 1

当我使用系统给出的建议答案时,我确实会看到一个模式弹出窗口。目标是让模态给我提供与我点击 map 标记相同的信息(见第一张图片)。当我最初点击搜索结果编号时,我得到以下信息:

blank modal

但是,当我点击一个 map 标记时,当我点击结果链接时,它的模态弹出,每个弹出的模态将显示最后弹出的 map 标记模态的信息,这意味着模态看起来正确,如第一张图片对于搜索结果中的每个链接都是相同的模态。

更新 2

我认为我的问题是,这些链接不是 map 标记,因此编写的 javascript 不适用于它们。我怎样才能使链接与 map 标记的行为相同,以便将信息传输到模式。

更新 3

我正在尝试按照建议将函数拉出并单击以调用它,到目前为止我有这个并且它不起作用或我。

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

最佳答案

目前你的链接是这样的:

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

这是创建指向您的/maps/{id} url 的基本链接。

要打开带有链接的引导模式,请执行以下操作:

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


更新:

首先,您的 initialize函数已经用 $('#myModal').modal('show') 显示模态这样你就可以去掉 data: {toggle: "modal", target: "#myModal"}来自链接。

接下来,您的 initialize函数需要将特定的 map 标记元素传递给它,所以我建议这样做:

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

然后在你的 javascript 中:

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

关于javascript - Link_to 标签显示 map 模态 Rails 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com