从网站API获取数据的正确格式

问题描述 投票:0回答:1

我试图将网站API中的数据提取到我的react / leaflet应用程序中。到目前为止,我在控制台中返回了数据:

    fetch('https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50')
          .then(res => res.json())
          .then(members => {
            console.log(members);
            this.setState({
              Nearbymems : members.Nearbymems
            });
          });

然后我尝试使用以下内容在地图上显示成员作为标记:

  {this.state.NearbyMems.map(members => (
           <Marker
                   position={[members.location.latitude, members.location.longitude]}
                   icon={myIcon3} >
              <Popup>
              <em>{members.name}, </em>
                  {members.bio} {'\n'}

                   <PopupModal initialModalState={true}/>
              </Popup>
           </Marker>
         ))}

虽然这没有产生任何错误,我的应用程序正在编译并正常运行,但没有显示任何标记。这是返回的JSON数据的示例,如:

{
"latlon": "53,-6",
"radius": "1000",
"format": "json",
"perpage": "50",
"pages": 108,
"page": 1,
"total": 5391,
"members": [
    {
        "id": 85639,
        "name": "____",
        "url": "https://thesession.org/members/85639",
        "location": {
            "latitude": "____",
            "longitude": "_____"
        },
        "date": "",
        "bio": ""
    },

编辑:

myIcon3的代码:

var myIcon3 = L.icon({
    iconUrl: 'http://www.libpng.org/pub/png/img_png/pengbrew_160x160.png',
    iconSize: [25, 51],
    iconAnchor: [12.5, 51],
    popupAnchor: [0, -51],
});

获取输出:

Object
  format: "json"
  latlon: "53,-6"
  members: (50) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, 
  {…}, {…}, {…},{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}
  {…}, {…},{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, 
  {…}, {…},{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
  page:   1
  pages:  108
  perpage: "50"
  radius:  "1000"
  total :   5391
  __proto__: Object
reactjs leaflet
1个回答
0
投票

解决我自己的问题。我正在使用名为IPAPI的IP查找程序,它要求我使用以下格式来格式化其lat / lng:

lng: location.longitude
lat: location.latitude

此功能用于捕获用户IP,如果他们拒绝谷歌内置位置请求。我已经删除了这个功能,我的fetch语句现在返回正确的数据。

© www.soinside.com 2019 - 2024. All rights reserved.