0

in my javascript code, i have code like this :

var myLocation = new google.maps.LatLng(52.585701,20.453212); 
var allowBounds = new google.maps.LatLngBounds(
   new google.maps.LatLng(52.584903,20.451171),
   new google.maps.LatLng(52.589701,20.463865)
);
if (allowBounds.contains(myLocation)) 
  console.log('allowed');
else
  console.log('disallowed');

it's work, the result is 'allowed', because i just use 2 parameter for allowBounds (southWest and northEast point).
now i have kml file with polygon coordinats more than 2 coordinat. i want use those coordinats for allowBounds paramater. because i want to check whether myLocation coordinat is in polygon location/area or not. mybe like this :

var allowBounds = new google.maps.LatLngBounds(
   new google.maps.LatLng(52.584903,20.451171),
   new google.maps.LatLng(52.589701,20.463865),
   new google.maps.LatLng(52.629701,20.413865),
   new google.maps.LatLng(52.572190,20.963865)
); 

that is possible?? if not, can you give me some advice to check myLocation coordinat is in polygon area or not without use google.maps.LatLngBounds??
thank you for your helped.

user2419303
  • 33
  • 1
  • 7
  • Check `containsLocation()` method from [geometry library](https://developers.google.com/maps/documentation/javascript/reference#poly) – Anto Jurković May 26 '14 at 09:27
  • @AntoJurković Nice, that does exactly what he wants. Consider making a minimal example and posting it as an answer. [LatLngBounds](https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds) cannot be used here as it can only be used for a rectangle with exactly 2 arguments. – Daniël Knippers May 26 '14 at 09:29
  • thanks Anto Jurković.. thats what exactly i want. – user2419303 May 26 '14 at 10:16

1 Answers1

0

According to the documentation you can't put more than 2 coordinates in the constructor. However, you can add more than 2 coordinates to a LatLngBounds object by using the .extend(point:LatLng) function.

    var markers = [
        new google.maps.LatLng(1.234, 5.678),
        new google.maps.LatLng(1.234, 5.678),
        new google.maps.LatLng(1.234, 5.678)
    ];

    var bounds = new google.maps.LatLngBounds();
    for (index in markers) {
        bounds.extend(markers[index]);
    }
ZippyV
  • 12,540
  • 3
  • 37
  • 52
  • 1
    The LatLngBounds will always be a rectangle. LatLngBounds.contains will not reliably detect whether a point is in a polygon or not, unless the polygon is a rectangle also. – geocodezip May 26 '14 at 10:24