There are so many threads about this topic but I still haven't been able to find a suitable answer for my case...
Here is what I'm trying to do:
1) define a function to get geocodes with Google Maps API v3
2) call the function with the address in question in order to get the geocode for this particular address
The code works perfectly fine if I place
alert(address);
after
address = results[0].geometry.location;
As I need it as a global variable, this is unfortunately not an option. When trying to use the 'address' variable as a global function, I always get as value 'undefined'.
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<script type="text/javascript">
var geocoder;
var address;
address = 'Frankfurt, Germany';
function codeAddress() {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
address = results[0].geometry.location;
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
codeAddress(address);
alert(address);
</script>
</body>
</html>
Anybody able to help me with this? Staring at the code doesn't seem to get me anywhere. I appreciate all help available, thanks!!!