tm4321 / jquery.geocomplete

A simple plugin for Google Maps Autocomplete.
http://projects.trentmentink.com/jquery_geocomplete/
MIT License
16 stars 8 forks source link

add fields type "function" with an argument for more interaction; #3

Closed fas3r closed 6 years ago

fas3r commented 6 years ago

not sure if that's helpful for someone else but I needed something more dynamic for the "field" options and it was better than using onChange event.

usage :

$("#userInput").geocomplete({ fields : function(results) {
       console.log(results);
} })

or only specific value.

$("#userInput").geocomplete({ fields : function(results) {
       console.log(results.formatted_address);
} })

it's also possible to change the function dynamically, it prevents to initiate a new geocomplete :

$("#userInput").data('gmap.geocomplete').fields = function(results) { 
      console.log(results.place_id);
}

the argument is results. Where result will contain all the information from :

"results" will have all the value as follow :

administrative_area_level_1:"PA"
administrative_area_level_2:"Montgomery County"
administrative_area_level_3:"Whitemarsh Township"
country:"US"
formatted_address:"416, 1100 E Hector St, Conshohocken, PA 19428, USA"
lat:40.07595389999999
lng:-75.28769849999998
locality:"Conshohocken"
place_id:"ChIJUx7p1Yy-xokRr97Ob9uGKp8"
political:"US"
postal_code:"19428"
route:"East Hector Street"
street_number:"1100"
url:"https://maps.google.com/q=416,+1100+E+Hector+St,+Conshohocken,+PA+19428,+USA&ftid=0x89c6be8cd5e91e53:0x9f2a86db6fcedeaf"

source for the "each" function to parse the "address_components" https://stackoverflow.com/a/16429122/4167123 .

BR, fas3r

tm4321 commented 6 years ago

Hi fas3r,

I appreciate you wanting to help contribute to this project!

Unfortunately this is not a valid PR as it makes changes to one of the compiled files instead of the source file. I haven't really given any guidelines for contributing so I apologize if that was not clear.

Also, I'm not sure I fully understand what your PR is aiming to accomplish. By turning fields into a function that gets passed the place details you are essentially duplicating the onChange event.

If you just need to get the place details without relying on the onChange event you can just call the get place method.

$("#myInput").geocomplete("get place");