This nests Google Maps Event Listener for click or drag. How should I refactor the last part where it reuses the function getAddressComponents()? If there are other parts to be refactored, please do offer suggestions.

  function getAddressComponents() {
geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
clearValue();
if (results[0]) {
for (var i = 0; i < results[0].address_components.length; i++)
{
}
} else {
}
}
});
}

// Add drag listener to marker for reverse geocoding
});

// Add click listener to marker for reverse geocoding
marker.setMap(null);
});
});


The map will have existing marker, which is draggable and geocodable. But once I click on the map to create a marker (thus, removing the existing marker), that new marker as I drag it, can't be geocodable. That's why I have to nest the listener to get it working. What have I done wrong?

-

google.maps.event.addListener(marker, 'drag', fgetAddressComponents);

And you don't need to add it twice. No need to add it in the click handler again.
Otherwise it's mostly ok, except for the == (instead of ===), I just have styling nitpicks to say about this code.