Calculate the center point of multiple latitude/longitude coordinate pairs

后端 未结 21 1537
暖寄归人
暖寄归人 2020-11-27 09:27

Given a set of latitude and longitude points, how can I calculate the latitude and longitude of the center point of that set (aka a point that would center a view on all poi

21条回答
  •  眼角桃花
    2020-11-27 09:34

    Dart/Flutter Calculate the center point of multiple latitude/longitude coordinate pairs

    Map getLatLngCenter(List> coords) {
        const LATIDX = 0;
        const LNGIDX = 1;
        double sumX = 0;
        double sumY = 0;
        double sumZ = 0;
    
        for (var i = 0; i < coords.length; i++) {
          var lat = VectorMath.radians(coords[i][LATIDX]);
          var lng = VectorMath.radians(coords[i][LNGIDX]);
          // sum of cartesian coordinates
          sumX += Math.cos(lat) * Math.cos(lng);
          sumY += Math.cos(lat) * Math.sin(lng);
          sumZ += Math.sin(lat);
        }
    
        var avgX = sumX / coords.length;
        var avgY = sumY / coords.length;
        var avgZ = sumZ / coords.length;
    
        // convert average x, y, z coordinate to latitude and longtitude
        var lng = Math.atan2(avgY, avgX);
        var hyp = Math.sqrt(avgX * avgX + avgY * avgY);
        var lat = Math.atan2(avgZ, hyp);
    
        return {
          "latitude": VectorMath.degrees(lat),
          "longitude": VectorMath.degrees(lng)
        };
      }
    

提交回复
热议问题