最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is LatLng inside a polygon - Stack Overflow

programmeradmin0浏览0评论

I have a typical polygon set on a 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 Bermuda Triangle coords from google's documentation. I also have some random coords, 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 one happens to be inside the triangle, and the second one outside. These are just examples though, what I need is a way to find out, if a provided coordinate (not just one of these 2, any coordinte) is inside or outside the polygon (also, not always the Bermuda Triangle, not even a triangle - the polygons can have any number of latLng's). I've looked through the Google Maps API documentation, but I couldn't find any function that provides an answer to this question.

I have a typical polygon set on a 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 Bermuda Triangle coords from google's documentation. I also have some random coords, 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 one happens to be inside the triangle, and the second one outside. These are just examples though, what I need is a way to find out, if a provided coordinate (not just one of these 2, any coordinte) is inside or outside the polygon (also, not always the Bermuda Triangle, not even a triangle - the polygons can have any number of latLng's). I've looked through the Google Maps API documentation, but I couldn't find any function that provides an answer to this question.

Share Improve this question edited Oct 30, 2017 at 4:56 Scransom 3,3353 gold badges33 silver badges56 bronze badges asked Feb 26, 2013 at 19:20 ZiarnoZiarno 7,5725 gold badges37 silver badges41 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 9

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

containsLocation(point:LatLng, polygon:Polygon) boolean Computes whether the given point lies inside the specified polygon.

proof of concept fiddle

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>

If the Google Maps API doesn't have any methods specifically for this, then you'll need to find or write your own. It's a common problem in computer graphics, and if you Google "polygon hit testing" you'll find a wealth of information. This SO answer looks reasonably comprehensive.

In V3 API there is google.maps.geometry.poly.containsLocation static method.

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

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

Take a look at wikipedia Point in polygon page. There are couple of ways to determine if the point is inside the polygon. One of them is ray casting algorithm. This page has some examples on how to implement that in google maps.

发布评论

评论列表(0)

  1. 暂无评论