javascript - Drawing boundaries on a map with LeafletJS - Stack Overflow

I'm starting to play with code that can generate maps. I am now looking at OSM (OpenStreetMaps) as

I'm starting to play with code that can generate maps. I am now looking at OSM (OpenStreetMaps) as a great solution. Also LeafletJS makes it very easy to draw maps based on OSM. So far so good.

I would like to be able to draw an outline (boundary) of a county and am trying to understand how this process will look like. Do I first make a call to find coords and then pass them into Leaflet or is there a better way?

I can get boundaries using Nominatim API, but calling like this:

.html?state=tx&county=Lee

And I can draw area in Leaflet like this:

var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]).addTo(mymap);

So, am I overthinking or this is how it works?

I'm starting to play with code that can generate maps. I am now looking at OSM (OpenStreetMaps) as a great solution. Also LeafletJS makes it very easy to draw maps based on OSM. So far so good.

I would like to be able to draw an outline (boundary) of a county and am trying to understand how this process will look like. Do I first make a call to find coords and then pass them into Leaflet or is there a better way?

I can get boundaries using Nominatim API, but calling like this:

https://nominatim.openstreetmap/ui/search.html?state=tx&county=Lee

And I can draw area in Leaflet like this:

var polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]).addTo(mymap);

So, am I overthinking or this is how it works?

Share edited Dec 16, 2020 at 20:01 santa asked Dec 16, 2020 at 19:38 santasanta 12.5k51 gold badges161 silver badges266 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can create a single function to get the county geometry and add it to map. Try the following code:

function drawCountyBoundary(county, state)
{
    url = `https://nominatim.openstreetmap/search.php?county=${county}&state=${state}&polygon_geojson=1&format=jsonv2`
    fetch(url).then(function(response) {
    return response.json();
  })
  .then(function(json) {
    geojsonFeature = json[0].geojson;
    L.geoJSON(geojsonFeature).addTo(map);
  });
}

drawCountyBoundary('Lee', 'Tx')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742293670a4416684.html

相关推荐

  • javascript - Drawing boundaries on a map with LeafletJS - Stack Overflow

    I'm starting to play with code that can generate maps. I am now looking at OSM (OpenStreetMaps) as

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信