I want to be able to catch the marker object outside the geocode function and cant figure out how to.
Please help me.
The code:
geocoder.geocode({ address: address },
function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results.length) {
if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
}
}
});
Thanks in advance!
Just declare your
markervariable outside the callback and assign it a value inside the callback:Be careful though, the callback is asynchronous so you won’t have anything useful in
markeruntil the callback has been triggered.