[英]Using google places search autocomplete in a rails app
我想在Rails表单中使用Google Places自动完成功能。
我正在使用Rails v5.1.1
我正在关注这篇文章: https : //developers.google.com/maps/documentation/javascript/examples/places-searchbox
我的应用程序布局如下:
...lots of layout code
<% yield %>
<!-- this is to yield for additional page specific JS from other external sources -->
<%= yield :additional_page_js %>
<%= yield :additional_js %>
</body>
然后,我有一个名为trips / show.html.erb的视图:
<div class="container">
<div class="col-xs-6">
<div class="col-xs-6">
<h4>Let's make a new leg</h4>
<%= simple_form_for(leg, html: { class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<%= f.input :journey_id, as: :hidden %>
<%= f.input :name, label: 'Give your leg a name', error: 'A name is mandatory - name it!' , hint: 'Something friendly...' %>
<%= f.simple_fields_for :origin do |p| %>
<%= p.input :name , label: 'Origin', error: 'An origin is mandatory - where did you begin?' , hint: 'Where did you begin?', :input_html => { :id => "pac-input", :class => "controls" } %>
<% end %>
<%= f.simple_fields_for :destination do |n| %>
<%= n.input :name , label: 'Destination', error: 'A destination is mandatory - where did you end up?' , hint: 'Where did you end up?'%>
<% end %>
<%= f.button :submit , "Lets go!" %>
<% end %>
</div>
<div class="col-xs-6">
<div id="origin_map"></div>
</div>
</div>
<%= content_for :additional_page_js do %>
<%= javascript_include_tag "controllers/journeys/journeys" %>
<% end %>
<%= content_for :additional_js do %>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAP927IOgIj5GshStjMVfGTfMzHj2Dr1uo&libraries=places&callback=initMapAutocomplete"></script>
<% end %>
请注意, content_for
'产量具有以下内容(即trips.js):
function initMapAutocomplete() {
var map = new google.maps.Map(document.getElementById('origin_map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13,
mapTypeId: 'roadmap'
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
content_for
具有以下内容:
travels.css的css具有以下内容:
#description {
font-family: Roboto;
font-size: 15px;
font-weight: 300;
}
#infowindow-content .title {
font-weight: bold;
}
#infowindow-content {
display: none;
}
#map #infowindow-content {
display: inline;
}
.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Roboto;
}
#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls {
display: inline-block;
padding: 5px 11px;
}
.pac-controls label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#pac-input:focus {
border-color: #4d90fe;
}
#title {
color: #fff;
background-color: #4d90fe;
font-size: 25px;
font-weight: 500;
padding: 6px 12px;
}
#target {
width: 345px;
}
页面加载时-pac-input字段不显示-实际上,甚至在生成的HTML中也完全没有:
<%= p.input :name , label: 'Origin', error: 'An origin is mandatory - where did you begin?' , hint: 'Where did you begin?', :input_html => { :id => "pac-input", :class => "controls" } %>
我没有任何JavaScript错误,并且在服务器日志中也没有Rails错误。
如果我从trips.js中删除了javascript-原始表单字段显示工作正常。 我相信导致该字段“消失”的那一行是来自travels.js的那一行:
var searchBox = new google.maps.places.SearchBox(input);
请有人帮我让这个Google Places搜索自动完成字段工作!
问题原来有两个部分。
1 /地图div没有尺寸,因此没有显示。 通过更改html使其具有大小-在页面上加载现在呈现的地图。
<div id="origin_map" style='width: 100%; height: 30rem;'></div>
2 /然后,我意识到我的输入字段没有消失-而是移到了地图顶部。 通过注释掉进行此重新定位的行-我能够通过可正常使用Google Places自动完成功能来修复表单。
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.