Answer the question
In order to leave comments, you need to log in
Why isn't the Google Maps API (Place autocomplete) method being called?
Hello! I am implementing RequireJS into a project under development, some difficulties have appeared.
In one of the modules there is a piece responsible for initializing the Place Autocomplete in the corresponding field:
define(
['jquery'],
function($) {
return {
...
autocomplete_init : function(autocomplete_element, place_id_element) {
var input = $('#'+autocomplete_element);
autocomplete = new google.maps.places.Autocomplete(input,
{ types: ['(cities)'] }
);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
// записываем id места в скрытый элемент формы (для передачи в пхп)
$('#'+place_id_element).val(place.place_id);
});
},
...
}
}
);
require.config({
baseUrl: 'js/lib',
paths: {
'app': '../app',
jquery: 'http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min',
async: 'require/async',
'maps-api': 'https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places',
},
shim: {
'maps-api': {
deps: []
},
'destinations-map': {
deps: ['jquery', 'async!maps-api']
},
},
});
require(
['jquery', 'app/form', 'destinations-map'],
function($, form){
...
// form - это модуль, в котором хранится показанная выше функция
form.autocomplete_init('location_input', 'destination_place_id');
}
);
autocomplete = new google.maps.places.Autocomplete(input,
{ types: ['(cities)'] }
);
Answer the question
In order to leave comments, you need to log in
You pass a jQuery object as the first parameter to the autocomplete constructor, and it expects an HTMLInputElement .
autocomplete = new google.maps.places.Autocomplete(input[0],
{ types: ['(cities)'] }
);
- should solve the problem. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question