【问题标题】:Angular httpClient get users from an Array of JSON using Longitude Latitude Haversine formulaAngular httpClient使用经度纬度Haversine公式从JSON数组中获取用户
【发布时间】:2020-06-15 11:58:51
【问题描述】:

我有一个 JSON 文件中的用户列表

[
  {
      "id": 1,
      "first_name": "Maurise",
      "last_name": "Shieldon",
      "latitude": 34.003135,
      "longitude": -117.7228641
  },
  {
      "id": 2,
      "first_name": "Bendix",
      "last_name": "Halgarth",
      "latitude": -2.9623869,
      "longitude": 104.7399789
  },
  {
      "id": 3,
      "first_name": "Meghan",
      "last_name": "Southall",
      "latitude": "15.45033",
      "longitude": "44.12768"
  },
  {
      "id": 4,
      "first_name": "Sidnee",
      "last_name": "Silwood",
      "latitude": -26.94087,
      "longitude": 29.24905
  },
  {
      "id": 5,
      "first_name": "Rosita",
      "last_name": "Ferrulli",
      "latitude": 33.5719791,
      "longitude": -84.3396421
  }]

我正在使用 Haversine 公式计算距离,因此我只能获取某些 LAT 和 LONG 值的用户,此方法在我的 api.service.ts 类中。

   getDistanceFromLatLon(lat1: number, lon1: number, lat2: number, lon2: number): number {
    var deg2Rad = deg => {
      return deg * Math.PI / 180;
    }
    var r = 3959; // Radius of the earth in miles
    var dLat = deg2Rad(lat2 - lat1);
    var dLon = deg2Rad(lon2 - lon1);
    var a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(deg2Rad(lat1)) * Math.cos(deg2Rad(lat2)) *
      Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    var d = r * c; // Distance in miles
    return d;
  }

然后在我的app.component.ts 文件中,我可以调用 JSON 数组,它会从上面的用户列表中为我提供data.json 文件中所有用户的列表。

getUsers() {
this.httpClient.get('/assets/users.json').pipe(map(data => data as Array<Users>))
      .subscribe(result => { console.log(result)}

在我从我的 JSON 文件中收到所有用户的列表之后。我正在尝试通过getDistanceFromLatLon 方法运行JSON result 数组,以便只有LONDON_LAT 和LONDON_LONG 的用户才能显示


 //Latitude and longitude of London
  LONDON_LAT = 51.509865;
  LONDON_LONG = -0.118092;
  miles;

const distance = this.apiService.getDistanceFromLatLon(result['latitude'], result['longitude'], 
this.LONDON_LAT, this.LONDON_LONG);
        if (distance <= this.miles) {
          this.UsersByRadius = result;
        }
      });
}

通过.getDistanceFromLatLon 编译后,我的this.UsersByRadius = result 是空的,我没有得到任何用户。 我基本上是想在我的角度应用程序中复制这个PHP application。任何帮助都感激不尽。

【问题讨论】:

    标签: php json angular typescript httpclient


    【解决方案1】:
    1. Angular SPA 通常在客户端生成。因此,在处理大型数组集时,所有的数学计算(单个除法运算的开销很大,更不用说所有的三角函数了)都会严重过载。您应该考虑在服务器端进行。

    2. 您正在检索一个数组。所以你需要遍历它们中的每一个来调用函数。

    getUsers() {
      this.httpClient.get('/assets/users.json').pipe(
        map(data => data as Array<Users>)
      ).subscribe(result => { 
        console.log(result);
        result.forEach(item => {
          const distance = this.apiService.getDistanceFromLatLon(item['latitude'], item['longitude'], this.LONDON_LAT, this.LONDON_LONG);
          if (distance <= this.miles) {
            this.UsersByRadius.push({
              id: item['id'],   
              first_name: item['first_name'],
              last_name: item['last_name'],   
              latitude: item['latitude'],   
              longitude: item['longitute'],
              city: 'London'
            });
          }
        });
      });
    }
    

    【讨论】:

    • 感谢您的建议,它似乎在 if 语句 ``` if (distance getDistanceFromLatLon 似乎没有编译结果仍然为空: (
    • 我已编辑答案以将const distance 移动到循环内。但是这里仍然存在一个问题。 this.UsersByRadius 将针对数组的每个项目单独更新。您需要考虑将 UsersByRadius 设为数组或传入对象的属性。
    • 谢谢。 UserByRadius 实际上是一个数组UsersByRadius: Array&lt;Users&gt;;我不确定miles 应该是什么属性,result 似乎正在打印所有users 而不是UsersByRadius
    • 已经这样做了。是我还在打印所有用户UsersByRadius = [];
    • 你需要将对象推送到UsersByRadius数组,我已经修改了代码。但我仍然没有看到 Users 类型定义。没有它,我无法说出您需要推送到阵列的具体内容。现在,我已经在答案中编写了占位符 Users 类型的对象。它应该替换为实际对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多