如何可视化地址的地理编码?在理想情况下知道这是否在我的多边形内?

How to visualize the geocode of an adress ? and in ideal situation knowing if this is inside my polygon?

我正在编写一个脚本来测试某个地址是否在某个区域中,例如某个点附近。我对服务器使用 node.js,然后对 API 地图框使用 JavaScript。我显示带有多边形(区域)、中心点和自动完成地址搜索的地图。
但现在我想知道,如何从地址中获取地理编码(纬度和经度)?
我想用这个坐标来计算这些坐标是否在我的多边形内,例如使用这些方法之一:
http://geomalgorithms.com/a03-_inclusion.html#wn_PinPolygon()

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Adress in/out area</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.3.1/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>
  <script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.1/mapbox-gl-geocoder.min.js'></script>
  <link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-geocoder/v4.4.1/mapbox-gl-geocoder.css' type='text/css' />
  <div id='map'>  </div>
  <script>
  mapboxgl.accessToken = 'mytoken';
  var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v11',
      center: [-73.943851,40.720021],
      zoom: 13
  });
   var geocoder =new MapboxGeocoder({
  accessToken: mapboxgl.accessToken,
  mapboxgl: mapboxgl
  });
  map.addControl(geocoder);
  geocoder.on('result', function(result) {
  console.log(result);
  });
  map.on('load', function () {
    map.addSource("checked-area", {
      "type": "geojson",
      "data": {
        "type": "FeatureCollection",
        "features": [{
          "type": "Feature",
          "geometry": {
            "type": "Polygon",
            "coordinates": [
            [
            [-73.9346845,40.7284786],
            [-73.9382465,40.7281208 ],
            [-73.9457138,40.7274053],
            [-73.9517648,40.725454],
            [-73.9560993,40.721486],
            [-73.9564855,40.710394],
            [-73.9406068,40.7118904],
            [-73.9330966,40.713582],
            [-73.9347274,40.7172251],
            [-73.933955,40.7202501],
            [-73.9351995,40.722722],
            [-73.9282472,40.7243482],
            [-73.9298351,40.727633],
            [-73.9346845,40.7284786]
            ]
            ]
          }
        }, {
          "type": "Feature",
          "geometry": {
            "type": "Point",
            "coordinates": [-73.943851,40.720021]
          }
        }]
      }
      });
      map.addLayer({
      "id": "area-boundary",
      "type": "fill",
      "source": "checked-area",
      "paint": {
      "fill-color": "#088",
      "fill-opacity": 0.5
      },
      "filter": ["==", "$type", "Polygon"]
      });
      map.addLayer({
        "id": "area-store",
        "type": "circle",
        "source": "checked-area",
        "paint": {
        "circle-radius": 6,
        "circle-color": "#B42222"
        },
        "filter": ["==", "$type", "Point"],
      });
    });
  </script>

</body>
</html>

这部分代码更特别,它处理地址的搜索部分:

  var geocoder =new MapboxGeocoder({
  accessToken: mapboxgl.accessToken,
  mapboxgl: mapboxgl
  });
  map.addControl(geocoder);
  geocoder.on('result', function(result) {
  console.log(result);
  });

我想从这段代码中获取坐标,以便能够计算它们是否在定义的区域内(并且在未来甚至不显示地图,只显示一条消息)。
我尝试了 JSON.parse(result) 并且我有:

Uncaught SyntaxError: Unexpected token o in JSON

我也试过 result.geometry.coordinates 和结果 ['geometry']['coordinates']
如果你有想法,你会帮助我很多:)
PS:如果出现负数,请告诉我原因,以便我改进 post :)

Uncaught SyntaxError: Unexpected token o in JSON

这主要是因为您试图解析一个对象而不是 JSON 字符串。您是否尝试过访问 result.result.geometry。这给出了几何。

    geocoder.on("result", result => {
      console.log(result.result.geometry);
    });

您还可以使用 turf.inside [1]

检查点几何是否位于多边形内

这是一个代码笔:https://codepen.io/manishraj/full/bGbxOVG。尝试搜索 Pune, IndiaDelhi, India.

[1] https://github.com/Turfjs/turf-inside