繁体   English   中英

如何获取 json 的纬度和经度属性,然后将它们转换为数组?

[英]How can I get the latitude and longitude properties of my json and then transform them into an array?

I have a json with several properties which interest me are the latitude and longitude of each object and then create a polyline with leaflet the json is this:

   {
        "id": 201,
        "clientid": "cf1",
        "description": "devices 4",
        "latitude": -22.33085,
        "longitude": -68.8210916,
    },
    {
       "id": 202,
       "clientid": "Cf2",
       "description": "devices 5",
       "latitude": -22.3309583,
       "longitude": -68.8212233,
       
    },
    {
        "id": 203,
        "clientid": "Cf3",
        "description": "devices 6",
        "latitude": -22.3343583,
        "longitude": -68.8252116,
     },
    {
        "id": 204,
        "clientid": "Cf4",
        "description": "devices 7",
        "latitude": -22.3358783,
        "longitude": -68.82457,
     },
    {
       "id": 201,
       "clientid": "Cf4",
       "latitude": -22.341195,
       "longitude": -68.8270966,
     }

以下代码显示了我如何获取数据以及如何拥有组件

    const getAllData = () => {
     try {
      getData()
      .then(res => {
      let data = res.body;
      info = data;
      console.log(info);
      return info;
    })
    } catch (error) {
      console.error(error);
  }
}


const options = {
  center: [-23.76875, -70.304484],
  zoom: 13,
  };

     <Map data={options}>
      {#each info as item }
        <Maps
        id={item.id}
        clientid={item.clientid}
        latitude={item.latitude}
        longitude={item.longitude}
        description={item.description}
        />
        {/each}
     </Map>

在我的 map 组件内部,我有以下逻辑,这是我必须获取经度和纬度数组以将其传递给折线组件并绘制线条的地方,这是我不太明白该怎么做

  export let id;
  export let clientid;
  export let latitude;
  export let longitude;
  export let description;



const tileUrl = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png";

  const tileLayerOptions = {
    minZoom: 4,
    maxZoom: 22,
    maxNativeZoom: 19,
    
  }
 
  let latLngs = [[latitude,longitude],[latitude,longitude]]

 <TitleLayer url={tileUrl} options={tileLayerOptions}/>
  <Market latLng={[ latitude, longitude]}>
 <PopUp>
  <p>
   Description: {description}
  </p>
  </PopUp>
  </Market>
<Polyline latLngs={[latLngs]}/>

  

在 leaflet 中, LatLngExpression等于[lat, lng]所以在你的情况下它给出let latLngs = [latitude, longitude]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM