How to find nearest location using latitude and longitude from a json data

前端 未结 3 1216
无人共我
无人共我 2021-02-06 15:19

I\'m trying to make a website that ask for user\'s location then find the closest location (100m radius) from it\'s position using GeoLocation and display the result in HTML.

3条回答
  •  一向
    一向 (楼主)
    2021-02-06 15:47

    To calculate the distance between two points (latitude, longitude), implemented a function of haversine formula in typescript.

    //There are 6200 points in the JSON file
    import data from './json/test.json';
    
    let radians = function (degree: number) {
    
      // degrees to radians
      let rad: number = degree * Math.PI / 180;
    
      return rad;
    }
    
    const haversine = (lat1: number, lon1: number, lat2: number, lon2: number) => {
    
      let dlat, dlon, a, c, R: number;
    
      R = 6372.8; // km
      dlat = radians(lat2 - lat1);
      dlon = radians(lon2 - lon1);
      lat1 = radians(lat1);
      lat2 = radians(lat2);
      a = Math.sin(dlat / 2) * Math.sin(dlat / 2) + Math.sin(dlon / 2) * Math.sin(dlon / 2) * Math.cos(lat1) * Math.cos(lat2)
      c = 2 * Math.asin(Math.sqrt(a));
      return R * c;
    }
    
    let test = function () {
      const keys = Object.keys(data);
    
      let count: number = keys.length;
      for (var _i = 0; _i < count; _i++) {
        var _dummy: number = haversine(
          36.12, -86.67, data[_i].position.lat, data[_i].position.lng);
      }
    
    }
    

提交回复
热议问题