php - Angular httpClient get users from an Array of JSON using Longitude Latitude Haversine formula

one text

Solution:

  1. Angular SPA are usually generated client-side. So all the mathematical calculations (a single division operation has a heavy overhead, not to mention all the trig functions) will be heavy overload when dealing with large array sets. You should consider doing it server-side.

  2. You are retreiving an array. So you need to loop through each of them to call the function.

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'
        });
      }
    });
  });
}

Source