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

时间:2015-02-26 19:53:22

标签: javascript ruby-on-rails ruby-on-rails-4 google-maps-api-3 slim-lang

我正在使用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因为&gt; 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自动填充应该。问题是我想从地址自动完成功能中使用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"

如何获取表格以拯救城市?

赞赏任何意见/想法。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

更改了此行

.field#locality = f.text_field :city, placeholder: "City"

到此

.field = f.text_field :city, placeholder: "City", id: 'locality'

并且它像魅力一样工作:)