google maps api places autocomplete getPlace() not working -
i have problem google maps api places autocomplete. seems function getplaces()
not working described in documentation: autocomplete
returns details of place selected user if details retrieved. otherwise returns stub place object, name property set current value of input field.
taking following code example:
<html> <head> <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=places"></script> <script type="text/javascript"> initialize = function () { var input = document.getelementbyid('locationsearch'); var options = { types: ['(cities)'] }; autocomplete = new google.maps.places.autocomplete(input, options); } google.maps.event.adddomlistener(window, 'load', initialize); submitform = function () { searchplace = autocomplete.getplace(); console.log(searchplace.name); } </script> </head> <body> <form onsubmit="submitform();return false;"> <input type="text" id="locationsearch"/> <input type="button" value="search" onclick="submitform(); return false;"/> </form> </body> </html>
this simple code uses autocomplete feature in input="text"
element , after button next pressed should log place name in console. problem have when input
element has incomplete place selected. expect getplace()
function return place
object name property set current value of input
element. instead console logging error: uncaught typeerror: cannot read property 'name' of undefined
.
is there i'm doing wrong using places autocomplete or function not working correctly? thank you.
reason being google has used identifier 'locality' instead of usual 'name'.
so if use sub element 'locality' 'place', you can name of place.
code:
var val = place.address_components[0][componentform['locality']];
Comments
Post a Comment