I am trying to display a marker for an address that the user enters in a web form. The problem is that I am getting the following error
Cannot call method ‘getSouthWest’ of undefined
when I try to show the marker using the following code:
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latitude = results[0].geometry.location.lat();
var longitude = results[0].geometry.location.lng();
//Remove all markers from the map
clearMarkers();
//Display the new marker on the map
showFullAddressesOnMap(latitude, longitude, address);
//Center the map according to the marker
map.fitBounds(results[0].geometry.bounds);
}
});
function showFullAddressesOnMap(latitude, longitude, fullAddress) {
displayMarker(-1, "", "", "", fullAddress, true, latitude, longitude);
}
function displayMarker(appID, title, image, imageType, address, markerType, lat, lng) {
var listingLatLng = new google.maps.LatLng(lat, lng);
var marker = new google.maps.Marker({
position: listingLatLng,
map: map,
icon: markerType ? imgBluePin : imgGreenPin,
title: title + address
});
var content = GetContent(appID, title, image, imageType, address);
marker.info = new google.maps.InfoWindow({
content: content,
size: new google.maps.Size(50, 50)
});
google.maps.event.addListener(marker, 'click', function () {
closeAllWindows();
marker.info.open(map, marker);
});
bounds.extend(listingLatLng);
map.fitBounds(bounds);
markers.push(marker);
}
Also, the error does not happen every time. Here is an address that crashes the code.
France, Ile-de-France, Paris, 75006, Paris, 7 rue casimir delavigne
I think the error is caused when FitBounds is called. When the error happens the bounds that are returned by the geocoding is zoomed out to Province level.
What is the cause of the error? I need to zoom the map to street level when the user enters address with street name.
bounds isn’t defined somewhere.
You must define it in a scope where it is accessible by displayMarker, before you use it’s methods or use it as argument for methods: