Is LatLng inside a polygon

I have a typical polygon set on google map with some latLng's:

var bermudaTriangle = new google.maps.Polygon({ paths: [ new google.maps.LatLng(25.774252, -80.190262), new google.maps.LatLng(18.466465, -66.118292), new google.maps.LatLng(32.321384, -64.75737), ] }); bermudaTriangle.setMap(map); 

these are the roots of the Bermuda Triangle from Google documentation. I also have some random coordinates, saaay:

 var coord1 = new new google.maps.LatLng(26.194876675795218,-69.8291015625) var coord2 = new new google.maps.LatLng(33.194876675795218,-63.8291015625) 

the first is inside the triangle, and the second is outside. These are just examples, but I need a way to find out if the provided coordinate (and not just one of these 2, any coordination) is inside or outside the polygon (also not always the Bermuda triangle, even the triangle - the polygons can have any number of latLng). I looked at the Google Maps API documentation, but I could not find any function that provides an answer to this question.

+5
source share
4 answers

Use the containsLocation method (point: LatLng, polygon: Polygon) .

containsLocation (point: LatLng, polygon: Polygon) boolean Calculates whether the given point is inside the specified polyangular box.

proof of violin concept

code snippet:

 function checkInPolygon(marker, polygon) { var infowindow = new google.maps.InfoWindow(); var html = ""; if (google.maps.geometry.poly.containsLocation(marker.getPosition(), polygon)) { html = "inside polygon"; } else { html = "outside polygon"; } infowindow.setContent(html); infowindow.open(map, marker); } var map; var coord1 = new google.maps.LatLng(26.194876675795218, -69.8291015625); var coord2 = new google.maps.LatLng(33.194876675795218, -63.8291015625); function initialize() { var map = new google.maps.Map( document.getElementById("map_canvas"), { center: new google.maps.LatLng(37.4419, -122.1419), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }); bermudaTriangle.setMap(map); var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < bermudaTriangle.getPath().getLength(); i++) { bounds.extend(bermudaTriangle.getPath().getAt(i)); } bounds.extend(coord1); bounds.extend(coord2); var marker1 = new google.maps.Marker({ map: map, position: coord1 }); var marker2 = new google.maps.Marker({ map: map, position: coord2, }); map.setCenter(bounds.getCenter()); map.setZoom(3); checkInPolygon(marker1, bermudaTriangle); checkInPolygon(marker2, bermudaTriangle); } google.maps.event.addDomListener(window, "load", initialize); var bermudaTriangle = new google.maps.Polygon({ paths: [ new google.maps.LatLng(25.774252, -80.190262), new google.maps.LatLng(18.466465, -66.118292), new google.maps.LatLng(32.321384, -64.75737), ] }); 
 html, body, #map_canvas { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div id="map_canvas"></div> 

+8
source

If the Google Maps API does not have any methods specifically for this, you will need to find or write your own. This is a common problem in computer graphics, and if you google a "testing ground hit" you will find a lot of information. This SO answer looks pretty reasonable.

+2
source

The V3 API has a static google.maps.geometry.poly.containsLocation method.

See https://developers.google.com/maps/documentation/javascript/reference#poly

So you just need to pass the polygon and the point you want to check if it is inside the polygon or not.

+1
source

Look at the wikipedia dot on the polygon page . There are several ways to determine if a point is inside a polygon. One of them is the ray casting algorithm. This page provides examples of how to implement this on google maps.

0
source

All Articles