javascript - convert string into place google maps -


i have searchbox , maps, when user write place searchbox map should moved place user has selected.

and code:

var input = document.getelementbyid('addressinput'); var searchbox = new google.maps.places.searchbox((input)); var places = searchbox.getplaces(); google.maps.event.addlistener(searchbox, 'places_changed', function () {    places = searchbox.getplaces();    if (places[0].geometry.viewport) {       map.fitbounds(places[0].geometry.viewport);    } else {       map.setcenter(places[0].geometry.location);       map.setzoom(17); // why 17? because looks good.    } }); 

so when user change place in searchbox works fine

the problem when arrive page contains map first time because have string 'orlando' or 'chicago, illinois' (i string page) , put value searchbox

document.getelementbyid('addressinput').value='orlando'; 

but don't place specifics because not place_changed event

how can transform string place?

the reason not work (propably, dont see actual code) try search before parts of google maps finished loading. place code in idle event :

google.maps.event.addlisteneronce(map, 'idle', function(){     input.value='orlando';   input.focus();  }); 

and works. see fiddle -> http://jsfiddle.net/2l5t3/


update. if want automatic search and automatic selection of first search result / prediction suggest use google.maps.places.placesservice on pageload instead. change idle function :

google.maps.event.addlisteneronce(map, 'idle', function(){       var request = {         query: 'orlando'     };      service = new google.maps.places.placesservice(map);     service.textsearch(request, callback);      function callback(results, status) {         if (status == google.maps.places.placesservicestatus.ok) {             //grab first item, orlando, florida, usa             var place = results[0];             input.value = place.formatted_address;              map.setcenter(place.geometry.location);         }     } }); 

see -> http://jsfiddle.net/j2kn3/

the searchbox / places_changed works normal afterwards. called pageload , return same lat / lng if searched orlando , grabbed first prediction user.

i tried in 100 ways, seems impossible select first item on .pac-container without user action, tried dispatch events - no luck.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -