Because geocode
is asynchronous you're not going to be able to set MapCenter
at the place you want to because the results of the API call won't have returned at that point. I suggest you use a callback
to help you manage the call:
var MapCenter, map, x, y, z;
function geocode(callback) {
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.bounds);
callback();
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
geocode(function () {
MapCenter = map.getCenter();
x = MapCenter.lat(), y = MapCenter.lng(), z = 0;
});