I'm working on a page close enough to the one in google samples https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform and it works fine.
However I need to add one more feature is to set the value of the autocomplete by default to be the current user city.
I'm using the following code to get the city and the country of the logged in user using geolocation API in HTML5. However the challenge is to make the autocomplete accept this value as it's default value. When I try to put the value in the textbox directly the autocomplete consider it as a wrong value.
navigator.geolocation.getCurrentPosition(function(pos) {
var currentLocation = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
var geocoder = new google.maps.Geocoder();
var city = "";
var country = "";
geocoder.geocode({'latLng': currentLocation}, function(results, status, from) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
for (var i = 0; i < results[0].address_components.length; i++) {
for (var b = 0; b < results[0].address_components[i].types.length; b++) {
if (results[0].address_components[i].types[b] == "administrative_area_level_1") {
city = results[0].address_components[i];
break;
}
if (results[0].address_components[i].types[b] == "country") {
country = results[0].address_components[i];
break;
}
}
}
var fromDefault = city.long_name + " - " + country.long_name;
$('#address-from').val(fromDefault);
}
}
});
},
function(error) {
alert('Unable to get location: ' + error.message);
}, options);
Any idea how to manually fill in the default value for the autocomplete places textbox?