user2230470 user2230470 - 17 days ago 4x
Javascript Question

`if ( ...contains(arrayOfLatLngObjects[i]) )` producing TypeError: is not a function

Below is the relevant and initial code that I was using which generates this error:

var businesses = [
{lat: 49.240020, lng: -123.076069},
{lat: 49.251947, lng: -123.167336},
var withinBounds = [];
function getMapBounds() {
mapBounds = myMap.getBounds();
for (var i = 0; i < businesses.length; i++) {
if (latLngClass.contains(businesses[i])) { // the condition is generating the error

After doing some digging around in the documentation, I realized that
is a method of the LatLngBounds class, so I assumed that this is what was causing the issue. However, I changed the code above to reflect the code below, but I'm still receiving the exact same error from the
if (condition)

var latLngClass = new google.maps.LatLngBounds(, mapBounds.lng);
for (var i = 0; i < businesses.length; i++) {
if (latLngClass.contains(businesses[i])) {

What I'm trying to do is create an array, which I'll use to place markers on the map.


LatLngBounds.contains expects object of type LatLng.

LatLng indeed exposes lat() and lng() functions which your basic JavaScript object does not provide.

Proper fix - pass LatLng object to contains:

if (latLngClass.contains(new google.maps.LatLng(businesses[i])) ...

or fill in businesses with LatLng instances to start with:

var businesses = [
    new google.maps.LatLng({lat: 49.240020, lng: -123.076069}),
    new google.maps.LatLng({lat: 49.251947, lng: -123.167336}),

Hacky fix - if LatLngBounds.contains accepts values that are duck-typing compatible with LatLng than you can match signature of you businesses[i] values to signature of LatLng by having lat() and lng() functions instead of just values like

 var businesses = [
    lat: function(){return 49.240020;}, 
    lng: function(){return -123.076069;}