Atlaslog
Atlaslog / Examples / Turf.js / Point in Polygon
Turf.js · Runnable Example

Point in Polygon

Runnable Turf.js example: Point in Polygon. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All Turf.js examples
/demos/turf-point-in-polygon.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/turf-point-in-polygon.html
<!--
  示例:点在多边形内判断 — 随机生成点位,逐点用 pointInPolygon 分类
  核心逻辑:同一批坐标分别渲染成绿色与橙色,统计内外数量
  依赖:Turf.js 7 · jsDelivr CDN
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>点在多边形内判断</title>
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d;background:#fff}
  .bar{flex:0 0 auto;display:flex;gap:10px;align-items:center;padding:8px 12px;
       background:#f6f7f9;border-bottom:1px solid #e3e6ea;font-size:12px;color:#58606e;flex-wrap:wrap}
  .bar button{font:inherit;font-size:12px;padding:4px 10px;border-radius:4px;cursor:pointer;
       border:1px solid #ccd1d9;background:#fff;color:#16181d}
  .bar button:hover{background:#eef0f3}
  svg{flex:1 1 auto;min-height:0;width:100%;display:block;background:#fbfcfd}
  .out{flex:0 0 auto;display:flex;gap:20px;flex-wrap:wrap;padding:8px 12px;
       border-top:1px solid #e3e6ea;font-family:ui-monospace,Consolas,monospace;font-size:11.5px;color:#3a4250}
  .out b{font-weight:600;color:#1f5fd0}
  .k{display:inline-block;width:8px;height:8px;border-radius:50%;margin-right:5px}
</style>
</head>
<body>
<div class="bar">
  <button id="regen">重新采样</button>
  <span>200 个随机点,边界上的点算在内部</span>
</div>
<svg id="stage" viewBox="0 0 640 320" preserveAspectRatio="xMidYMid meet"></svg>
<div class="out">
  <span><span class="k" style="background:#17694a"></span>面内 <b id="in">—</b></span>
  <span><span class="k" style="background:#c2410c"></span>面外 <b id="out">—</b></span>
</div>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@7/turf.min.js"></script>
<script>
(function () {
  'use strict';

  if (typeof turf === 'undefined') {
    document.body.innerHTML =
      '<div style="padding:24px;color:#b42318">Turf.js 加载失败,请检查能否访问 cdn.jsdelivr.net</div>';
    return;
  }

  var W = 640, H = 320, PAD = 26;
  var stage = document.getElementById('stage');
  var inPoly = turf.pointInPolygon || turf.booleanPointInPolygon;

  var polygon = turf.polygon([[
    [116.280, 39.870], [116.350, 39.822], [116.462, 39.846],
    [116.520, 39.915], [116.472, 39.988], [116.352, 40.006],
    [116.268, 39.944], [116.280, 39.870]
  ]], { name: '演示区' });

  var BOX = [116.21, 39.78, 116.59, 40.05];
  var kx = Math.cos(((BOX[1] + BOX[3]) / 2) * Math.PI / 180);
  var x0 = BOX[0] * kx, x1 = BOX[2] * kx;
  var y0 = BOX[1], y1 = BOX[3];
  var s = Math.min((W - PAD * 2) / (x1 - x0), (H - PAD * 2) / (y1 - y0));
  var ox = (W - (x1 - x0) * s) / 2;
  var oy = (H - (y1 - y0) * s) / 2;

  function pr(c) {
    return [(c[0] * kx - x0) * s + ox, (y1 - c[1]) * s + oy];
  }

  function ringPath(ring) {
    var d = '';
    for (var i = 0; i < ring.length; i++) {
      var p = pr(ring[i]);
      d += (i ? 'L' : 'M') + p[0].toFixed(1) + ' ' + p[1].toFixed(1);
    }
    return d + 'Z';
  }

  function draw() {
    var coords = [];
    for (var i = 0; i < 200; i++) {
      coords.push([
        BOX[0] + Math.random() * (BOX[2] - BOX[0]),
        BOX[1] + Math.random() * (BOX[3] - BOX[1])
      ]);
    }

    var inside = 0;
    var parts = ['<rect x="0" y="0" width="640" height="320" fill="none" stroke="#e3e6ea"/>'];
    parts.push('<path d="' + ringPath(polygon.geometry.coordinates[0]) +
               '" fill="rgba(31,95,208,0.06)" stroke="#1f5fd0" stroke-width="1.5"/>');

    coords.forEach(function (c) {
      var hit = inPoly(turf.point(c), polygon);
      if (hit) inside++;
      var p = pr(c);
      parts.push('<circle cx="' + p[0].toFixed(1) + '" cy="' + p[1].toFixed(1) +
                 '" r="2.6" fill="' + (hit ? '#17694a' : '#c2410c') + '"/>');
    });

    stage.innerHTML = parts.join('');
    document.getElementById('in').textContent = inside;
    document.getElementById('out').textContent = coords.length - inside;
  }

  document.getElementById('regen').addEventListener('click', draw);
  draw();
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More Turf.js examples

Turf.js · EXAMPLEPoint in Polygon Batch Turf.js · EXAMPLEPoint Line Distance Turf.js · EXAMPLEPolygon to Line