I am doing that when I enter the address it shows me the latitude and longitude but I get null or empty, I don't know if I am missing something this is what I have:
function initMap(){
var map = new google.maps.Map(document.getElementById('map'), {
center: centerMap,
scrollwheel: false,
zoom: zoom
});
var geocoder = new google.maps.Geocoder();
$('#recoger').keyup(busquedaGoogle(geocoder));
}
function busquedaGoogle (geocoder) {
return function (event) {
var direccion = $(this).val();
console.log(direccion);
geocoder.geocode({'address': direccion}, function (results, status) {
if (status === google.maps.GeocoderStatus.OK){
console.log(results[0].geometry.location);
}
else{
console.log('Geocode was not successful for the following reason: ' + status);
}
});
}
}
console output:
Your code is failing for another reason. I guess it's because of the google ads bug, which prevents another part of the code from executing fully or partially.
I made a snippet with your code (adapted for the occasion) that does work.
However , the problem you are trying to solve is already solved. Google provides an address autocomplete widget, and it reacts to the event
place_changed
so you know when a user chose an address.The way you have it done now, although it works, you're going to run into a lot of errors for exceeding the request quota, since you do one for each key.
I leave you the example with your autocomplete and google.