Trigger places_changed in Google maps from submit button

蓝咒 提交于 2019-12-23 00:53:32

问题


I am wanting to trigger my placed_changed function when a submit button is clicked. So here is what I have, I have a searchbox I assign using google.maps.places.SearchBox and once I click enter with the search box on focus it triggers the event:

 google.maps.event.addListener(searchBox, 'places_changed', function() {
    runMainPopulation(searchBox.getPlaces());
  });

Which runs the next function. Now what I am trying to do is get this function to run once I click a search button or I click enter on my searchbox.

Suggestions, thoughts?


回答1:


I figured it out.

I had to initialize the Google searchBox via google method post.

For instance:

$(".submit-search").bind('click', function() {

    input.focus(); 
    var e = jQuery.Event("keydown");
    e.which = 13; // # Some key code value
    $(input).trigger(e);
    google.maps.event.trigger(searchBox, 'place_changed');
});

This would not work, I am not to sure as to why but it doesn't. submit-search is the button next to searchBox also defined as input.

Now if I initialize my input trigger focus and click through google.maps.event.trigger it works perfect:

$(".submit-search").bind('click', function() {
    google.maps.event.trigger(input, 'focus')
    google.maps.event.trigger(input, 'keydown', {
        keyCode: 13
    });
  });


来源:https://stackoverflow.com/questions/27829792/trigger-places-changed-in-google-maps-from-submit-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!