将Google Places API与Rails应用程序集成

Integrating Google Places API with a Rails Application

本文关键字:Rails 应用程序 集成 API Google Places      更新时间:2024-06-17

我在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'

它就像一个魅力:)