[英]Keep previous values when redirecting to a new page
I'm using the following javascript
to pick a point on the map and populate the address field with this point. 我正在使用以下
javascript
在地图上选择一个点,并使用此点填充地址字段。 The address field is used inside a rails from tag in order to search by the address: 地址字段用于标记中的rails中,以便按地址进行搜索:
<%= form_tag search_path, method: "get", id: "other-form" do %>
<%= text_field_tag :location, params[:location], id:"address" %>
<%= submit_tag "submit", :id=> "my-button" %>
<%= text_field_tag :search_other, params[:search] %>
<%= hidden_field_tag :lat %>
<%= hidden_field_tag :lng %>
<% en d%>
<div id="map_canvas"></div>
<script>
var map;
var geocoder;
var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP };
function initialize() {
var myOptions = {
center: new google.maps.LatLng(38.89103282648849, -97.646484375),
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
var marker;
function placeMarker(location) {
if(marker) { //on vérifie si le marqueur existe
marker.setPosition(location); //on change sa position
} else {
marker = new google.maps.Marker({ //on créé le marqueur
position: location,
map: map
});
var circle = new google.maps.Circle({
map: map,
radius: 24140, // 15 miles in metres
fillColor: '#08aa23'
});
circle.bindTo('center', marker, 'position');
}
document.getElementById('lat').value=location.lat();
document.getElementById('lng').value=location.lng();
getAddress(location);
var cookie_val = ('other_lat_lng', location.lat() + "," + location.lng())
document.cookie = "other_lat_lng=" + escape(cookie_val);
}
var a_message = "<%= I18n.t 'map_other' %>"
function getAddress(latLng) {
geocoder.geocode( {'latLng': latLng},
function(results, status) {
if(status = google.maps.GeocoderStatus.OK) {
if(results[0]) {
document.getElementById("address").value = results[0].formatted_address;
} else {
document.getElementById("address").value = a_message;
}
} else {
document.getElementById("address").value = status;
}
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
Then on the results page I have a link_to
which redirects to another page with the map and the search form. 然后在结果页面上,我有一个
link_to
,它使用地图和搜索表单重定向到另一个页面。 When redirecting, I would like this new page to keep the same point that was choosen in the previous search. 重定向时,我希望这个新页面保持上一次搜索中选择的相同点。
You can expose the :lat and :lng by passing them through the link_to, then exposing them to the JS side with some data tags. 您可以通过将:lat和:lng传递给link_to来公开它们,然后使用一些数据标签将它们公开给JS端。
<%= link_to "new link", new_page_path(lat: params[:lat], lng:
params[:lng]) %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.