SA__
SA__

Reputation: 1862

Finding whether the coordinate is within polygon

I want to know if the given coordinates (lat & lon) is within given 4 coordinates.

enter image description here

i.e., If i have the lat,lon of A,B,C,D and E and i wanted to check if E lies within A,B,C and E.

How can i do that ?

I tried to see this example from Google Map Coordinates LatLngBound class - contains.

The example i can see is like

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

It has only 2 coordinates, like A&B. Hows that possible without having C&D's coordinates ?

Upvotes: 0

Views: 1376

Answers (1)

R10t--
R10t--

Reputation: 829

It's because this is using LatLngBounds which creates a rectangle, not a polygon. If you have two corners of a box, the other two corners can be assumed. You just need to specify the opposite corners. In your example you just need to send either A and D or B and C as a pair, the other two can be ignored. If you need a polygon then you want to create a polygon and then get determine if it contains the point with google.maps.geometry.poly.containsLocation(point, polygon).

containsLocation() Documentation here.

Full example here.

Upvotes: 1

Related Questions