将Google Places API与Rails应用程序集成
Integrating Google Places API with a Rails Application
我在Rails 4应用程序中使用了一个地址自动完成表单字段。
这是Javascript:
// This example displays an address form, using the autocomplete feature
// of the Google Places API to help users fill in the information.
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
{ types: ['geocode'] });
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i++) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
和HTML(使用Slim,因为>HTML语法)
= form_for @post, :html => {:multipart => true} do |f|
.field#locationField = f.text_field :address, id: "autocomplete", placeholder:"Address", onFocus: "geolocate()"
.field#locality = f.text_field :city, placeholder: "City"
#locationField会自动完成。问题是,我想用Address自动完成函数中的City填充#locality字段。
我得到了它,这样它就可以填充一个单独的输入(在这个form_for标记之外)。这张表是什么样子的:
table#address
tr
td.label Street address
td.slimField
input.field#street_number disabled="true"
td.wideField colspan="2"
input.field#route disabled="true"
tr
td.label City
td.wideField colspan="3"
input.field#locality disabled="true"
tr
td.label State
td.slimField
input.field#administrative_area_level_1 disabled="true"
td.label Zip code
td.wideField
input.field#postal_code disabled="true"
tr
td.label Country
td.wideField colspan="3"
input.field#country disabled="true"
我怎样才能拿到我的表格来拯救这座城市?
欢迎提供任何意见/想法。
谢谢你抽出时间。
更改了这一行
.field#locality = f.text_field :city, placeholder: "City"
到此
.field = f.text_field :city, placeholder: "City", id: 'locality'
它就像一个魅力:)
相关文章:
- angularjs+rails应用程序中未显示模板
- 滚动到Rails 4应用程序中的顶部jQuery
- React Rails应用程序中动态子项的密钥分配问题
- 为什么Turbolinks不能正常工作?Rails应用程序
- 为什么Highchart在我的rails应用程序中加载了两次?(未捕获的Highcharts错误#16)
- 为我的rails应用程序显示javascript弹出消息
- 如何在rails应用程序中测试AJAX加载的内容
- Ruby On Rails应用程序与angularJS关于AJAX
- Ember.js+传单+rails(使用Javascript MVC创建rails应用程序并打开源代码映射)
- 在我的Rails应用程序中使用Stripe对卡充电时出现问题
- Basecamp的Trix WYSIWYG编辑器gem没有在Rails 4应用程序中保存文件附件
- Rails 应用程序(狂欢) - 突然的“未捕获的类型错误”
- 如何在咖啡脚本中识别提交按钮,Rails 3应用程序
- 如何使用ScriptTags开发用于shopify的rails应用程序
- Rails 4 应用程序 - Heroku / Cloudfront - 资产无法从源正确服务
- 如何在 Rails 应用程序中使用任何 JavaScript 代码
- 如何在 Rails 应用程序中向 Galleria 添加自定义描述
- 如何使用Bootstrap和Heroku为Rails应用程序添加隐藏的播放音频
- 是否有人在生产 Rails 应用程序中使用 Babel/6 到 5
- Rails应用程序中的每个javascript文件都需要.on('page:change',初始化)吗?