Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何根据经纬度在开放式街道地图中为城市着色?_Javascript_Leaflet_Openstreetmap - Fatal编程技术网

Javascript 如何根据经纬度在开放式街道地图中为城市着色?

Javascript 如何根据经纬度在开放式街道地图中为城市着色?,javascript,leaflet,openstreetmap,Javascript,Leaflet,Openstreetmap,我正在使用开放式街道地图在我的应用程序中显示地图。以下代码用于显示开放式街道地图 <div id="map" style="width: 90%; height: 420px"></div> 然而,地图显示了,但是我想根据经度和纬度在地图上给一个城市上色。我搜索了很多,找到了一些链接。比特我没有任何线索来实现这一点。如何根据纬度和经度为地图上的城市着色 我使用flask作为后端 使用https://nominatim.openstreetmap.org/要获取多边形坐

我正在使用开放式街道地图在我的应用程序中显示地图。以下代码用于显示开放式街道地图

 <div id="map" style="width: 90%; height: 420px"></div>
然而,地图显示了,但是我想根据经度和纬度在地图上给一个城市上色。我搜索了很多,找到了一些链接。比特我没有任何线索来实现这一点。如何根据纬度和经度为地图上的城市着色


我使用flask作为后端

使用
https://nominatim.openstreetmap.org/
要获取多边形坐标,但此API接受城市名称,因此需要使用反向gocoding将坐标转换为城市名称。您可以使用
esri传单地理编码器

 const geocoder = L.Control.Geocoder.nominatim();

  const geocodeService = L.esri.Geocoding.geocodeService();
  const yourCoordinates = [23.8103, 90.4125];

  geocodeService
    .reverse()
    .latlng(yourCoordinates)
    .language("eng")
    .run(function(error, result) {
      if (error) {
        return;
      }

      if (result && result.address) {
        fetch(
          `https://nominatim.openstreetmap.org/search.php?q=${
            result.address.City
          }&polygon_geojson=1&format=json`
        )
          .then(res => res.json())
          .then(res => {
            console.log(res[1].geojson);
            L.geoJSON(res[1].geojson).addTo(map);
          });
      }
    });

是否要使用多边形为城市“着色”?你说的“给城市着色”是什么意思?是的,我想用多边形。
 const geocoder = L.Control.Geocoder.nominatim();

  const geocodeService = L.esri.Geocoding.geocodeService();
  const yourCoordinates = [23.8103, 90.4125];

  geocodeService
    .reverse()
    .latlng(yourCoordinates)
    .language("eng")
    .run(function(error, result) {
      if (error) {
        return;
      }

      if (result && result.address) {
        fetch(
          `https://nominatim.openstreetmap.org/search.php?q=${
            result.address.City
          }&polygon_geojson=1&format=json`
        )
          .then(res => res.json())
          .then(res => {
            console.log(res[1].geojson);
            L.geoJSON(res[1].geojson).addTo(map);
          });
      }
    });