Google Maps v3 - Why is LatLngBounds.contains returning false

I have the following code in which I would expect the contains method to return true, but it returns false:

var bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(55.38942944437183, -2.7379201682812226),
    new google.maps.LatLng(54.69726685890506, -1.2456105979687226)
);

var center = bounds.getCenter();  // (55.04334815163844, -1.9917653831249726)

var x = bounds.contains(center);  // returns false

On the same page, where map is a reference to the Map object, the following code returns true as expected:

map.getBounds().contains(map.getBounds().getCenter())

Why might my call to bounds.contains be returning false?

Answers


Ah, brilliant. The google.maps.LatLngBounds constructor expects SouthWest and NorthEast LatLng parameters. I have somehow bungled up my coordinates and passed in NorthWest and SouthEast instead!

var bounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(54.69726685890506,-2.7379201682812226),
    new google.maps.LatLng(55.38942944437183, -1.2456105979687226)
);

var center = bounds.getCenter();  // still returns (55.04334815163844, -1.9917653831249726)

var x = bounds.contains(center);  // now returns true

Lesson learned: getCenter doesn't care if you created the LatLngBounds with NorthWest and SouthEast instead, but if you want contains to return a useful answer you better pass in the suggested SouthWest and NorthEast!


I guess its easier to try this. It works for me without having to worry about NE orSW

var bounds = new google.maps.LatLngBounds();
bounds.extend(54.69726685890506,-2.7379201682812226);
bounds.extend(55.38942944437183, -1.2456105979687226);
var center = bounds.getCenter();  // still returns (55.04334815163844, -1.9917653831249726)
var x = bounds.contains(center);  // now returns true

I know this post is old, but I came searching for answers here, so thought of updating from what I have learnt.


This is the way that it worked for me:

var bounds = new google.maps.LatLngBounds();
bounds.extend(54.69726685890506,-2.7379201682812226);
bounds.extend(55.38942944437183, -1.2456105979687226); 
map.fitBounds(bounds);    

Need Your Help

Java and HTTPS url connection without downloading certificate

java ssl https

This code connects to a HTTPS site and I am assuming I am not verifying the certificate. But why don't I have to install a certificate locally for the site? Shouldn't I have to install a certifi...

what is a projection in LINQ, as in .Select()

linq projection

I typically do mobile app development, which doesn't always have .Select. However, I've seen this used a bit, but I don't really know what it does or how it's doing whatever it does. It is anything...