Justin Tolchin Justin Tolchin - 6 months ago 15
Vb.net Question

Re-Load Google place data from cookie

I have implemented a version of http://www.aspsnippets.com/Articles/Google-Places-AutoComplete-example-without-using-Maps.aspx

It works great!

What I am trying to do now is to pull the last used location from a cookie. In the cookie, I store the text from the textbox that Google put there after the correct item was selected. This is the full name and street for the place. I can re-populate the textbox with the text data, but I cannot get it to recognize in Google. I tried triggering the change event with

$('#txtLocation').trigger('change');


which does not seems to fire.

When I click in the pre-populated field, Google gives me the correct entry in the list which I then need to click. Does anyone have a good way to do this?

Answer

You can trigger google autocomplete by using this on the textfield:

google.maps.event.trigger($('#txtLocation')[0], 'focus');

Additional information: This is one way to use the Autocomplete-Feature of Google-Maps, without the need of a textfield at all:

var locationService  = new google.maps.places.AutocompleteService(null, {
  types: ['geocode']
});

locationService.getPlacePredictions({ 
  input: "mylocation"
}, function(predictions, status) {
  if(status=='OK' && predictions.length > 0 && predictions[0]) {
    var s = new google.maps.places.PlacesService($('<div>')[0]);
    s.getDetails({
      reference: predictions[0].reference
    }, function(details, status) {
      var latitude = details.geometry.location.lat();
      var longitude = details.geometry.location.lng();
    });
  }
});
Comments