无限的Google商家信息实例

时间:2016-11-17 11:17:47

标签: javascript jquery html location google-places-api

我的数据库记录打印到一个表中,其值为输入值。对于每条记录,我都需要Google地址字段自动填充其API。

HTML:

<input name="edit_airplane[1][location]" type="text" class="airplane_location" value="Balmaceda">

使用Javascript:

function initAutocomplete() {
    var airplane_location_field = jQuery( '.airplane_location');

    autocomplete = new google.maps.places.Autocomplete( airplane_location_field ), {types : ['geocode']} );
    autocomplete.addListener( 'place_changed', function() {
        /* 
           Whenever the value of the location field has changed. I need to
           Create a new input field which is hidden with the full address 
           Of the selected google autocomplete location.
        */
    });
}

检查哪个输入字段已更改的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

有很多研究并最终得出了这个解决方案。

function initAutocomplete() {
    // All of the airplanes_location input fields are combined.
    var airplane_location_fields = jQuery( '.airplane_location' );

    // For each location form field 
    airplane_location_fields.each( function() {
        var _this = jQuery( this ), autocomplete = new google.maps.places.Autocomplete( this );
        _this.data('autocomplete', autocomplete);

        // For each input field I stored the autocomplete object into .data; 
        _this.data('autocomplete').addListener( 'place_changed', function() {
            getPlaceData( _this );
        } );
    } );
}


function getPlaceData( input ) {
    // In this function I checked the input.data('autocomplete');
}