簡體   English   中英

如何使用谷歌地圖檢測到一個點位於多邊形內?

[英]How to detect that a point is inside a Polygon using Google Maps?

我想檢測一下google.maps.LatLng是否在google.maps.Polygon

我怎樣才能做到這一點 ?

干杯,

你可以在谷歌地圖V3中使用它: -

google.maps.geometry.poly.containsLocation(google.maps.LatLng(latitude, longitude),polygons);

多邊形是在polygoncomplete之后由函數返回的對象。

var polygons=null;
google.maps.event.addDomListener(drawingManager, "polygoncomplete", function(polygon) {
        polygons=polygon;
});

參考https://developers.google.com/maps/documentation/javascript/reference

其他解決方案: Google-Maps-Point-in-Polygon

用於Polygon類的Javascript Google Maps v3擴展,用於檢測點是否位於其中...

Google在幾何庫中提供了自己的實現,我還沒有檢查過,但可能會涵蓋其他答案中討論的邊緣情況。

請參閱此處描述的containsLocation方法。 請注意,您必須顯式導入幾何庫,因為它不在基本映射API中

我用這個算法來檢測點是在多邊形內部: http//alienryderflex.com/polygon/

我添加了一個contains多邊形的新方法:

// Add a function contains(point) to the Google Maps API v.3

google.maps.Polygon.prototype.contains = function(point) {
  var j=0;
  var oddNodes = false;
  var x = point.lng();
  var y = point.lat();

  var paths = this.getPath();

  for (var i=0; i < paths.getLength(); i++) {
    j++;
    if (j == paths.getLength()) {j = 0;}
    if (((paths.getAt(i).lat() < y) && (paths.getAt(j).lat() >= y))
    || ((paths.getAt(j).lat() < y) && (paths.getAt(i).lat() >= y))) {
      if ( paths.getAt(i).lng() + (y - paths.getAt(i).lat())
      /  (paths.getAt(j).lat()-paths.getAt(i).lat())
      *  (paths.getAt(j).lng() - paths.getAt(i).lng())<x ) {
        oddNodes = !oddNodes
      }
    }
  }
  return oddNodes;
}

google.maps.Polyline.prototype.contains = google.maps.Polygon.prototype.contains;

這里描述的每種方法都以某種方式失敗。

consider polygons with geodesic edges. Andrei I和Natim給出的方法考慮具有測地邊緣的多邊形。 這些方法也未能意識到Google地圖中的非測地邊緣僅在墨卡托投影中是直的。 這些方法假設頂點位於相等距離的緯度/經度網格上,其中一度緯度等於一度經度。 由於此錯誤,這些方法將指示一個點在多邊形外部,而在某些情況下顯示在內部。 對於長的非垂直/非水平邊緣,這很容易觀察到。 要解決此問題,必須首先將所有點從緯度,經度轉換為墨卡托投影中的X,Y坐標。 這是在Mercator中將坐標從lat / lon轉換為x / y的方法。 (缺乏准確性) Rhumb線導航可以用作替代方法的基礎。 Google Maps實驗版3.10實現了此方法。

consider geodesic edges, but as of Google Maps v3.9 it uses the same method mentioned above for non-geodesic polygons. Paul Gibbs,swapnil udare和Adi Lester提到的方法考慮了測地邊緣,但是從谷歌地圖v3.9開始,它使用與上述非測地多邊形相同的方法。 因此,它也遭受上述相同的問題。

更新 - 谷歌地圖的問題已在當前的Google Maps v3.10實驗版中得到糾正。

不需要復雜的算法,我能夠使用html canvas的isPointInPath()方法實現這一點。

http://www.w3schools.com/tags/canvas_ispointinpath.asp

創建一個canvas元素。 使用moveTo(),lineTo()方法繪制具有多個端點的多邊形。 使用isPointInPath()方法驗證點(x,y)是否位於多邊形內部。

<canvas id="canvas"></canvas>

//x,y are coordinate of the point that needs to be tested
//coordinates contains all endpoint of a polygon in format of x1,y1,x2,y2,x3,y3....
function isPointInPolygon(x, y, coordinates) {
var ctx = canvas.getContext("2d");
var coords = coordinates.split(',');

if (coords != null && coords.length > 4) {
    ctx.beginPath();
    ctx.moveTo(coords[0], coords[1]);
    for (j = 2; j < coords.length; j++) {
        ctx.lineTo(coords[j], coords[j + 1]);
        j++;
    }
    ctx.closePath();
    if (ctx.isPointInPath(x, y))
        return true;
    else
        return false;
}
return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM