Calculate the center point of multiple latitude/longitude coordinate pairs

后端 未结 21 1353
暖寄归人
暖寄归人 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:53

    I did this task in javascript like below

    function GetCenterFromDegrees(data){
        // var data = [{lat:22.281610498720003,lng:70.77577162868579},{lat:22.28065743343672,lng:70.77624369747241},{lat:22.280860953131217,lng:70.77672113067706},{lat:22.281863655593973,lng:70.7762061465462}];
        var num_coords = data.length;
        var X = 0.0;
        var Y = 0.0;
        var Z = 0.0;
    
        for(i=0; i<num_coords; i++){
            var lat = data[i].lat * Math.PI / 180;
            var lon = data[i].lng * Math.PI / 180;
            var a = Math.cos(lat) * Math.cos(lon);
            var b = Math.cos(lat) * Math.sin(lon);
            var c = Math.sin(lat);
    
            X += a;
            Y += b;
            Z += c;
        }
    
        X /= num_coords;
        Y /= num_coords;
        Z /= num_coords;
    
        lon = Math.atan2(Y, X);
        var hyp = Math.sqrt(X * X + Y * Y);
        lat = Math.atan2(Z, hyp);
    
        var finalLat = lat * 180 / Math.PI;
        var finalLng =  lon * 180 / Math.PI; 
    
        var finalArray = Array();
        finalArray.push(finalLat);
        finalArray.push(finalLng);
        return finalArray;
    }
    
    0 讨论(0)
  • 2020-11-27 09:55

    If you want all points to be visible in the image, you'd want the extrema in latitude and longitude and make sure that your view includes those values with whatever border you want.

    (From Alnitak's answer, how you calculate the extrema may be a little problematic, but if they're a few degrees on either side of the longitude that wraps around, then you'll call the shot and take the right range.)

    If you don't want to distort whatever map that these points are on, then adjust the bounding box's aspect ratio so that it fits whatever pixels you've allocated to the view but still includes the extrema.

    To keep the points centered at some arbitrary zooming level, calculate the center of the bounding box that "just fits" the points as above, and keep that point as the center point.

    0 讨论(0)
  • 2020-11-27 09:56

    If you are interested in obtaining a very simplified 'center' of the points (for example, to simply center a map to the center of your gmaps polygon), then here's a basic approach that worked for me.

    public function center() {
        $minlat = false;
        $minlng = false;
        $maxlat = false;
        $maxlng = false;
        $data_array = json_decode($this->data, true);
        foreach ($data_array as $data_element) {
            $data_coords = explode(',',$data_element);
            if (isset($data_coords[1])) {
                if ($minlat === false) { $minlat = $data_coords[0]; } else { $minlat = ($data_coords[0] < $minlat) ? $data_coords[0] : $minlat; }
                if ($maxlat === false) { $maxlat = $data_coords[0]; } else { $maxlat = ($data_coords[0] > $maxlat) ? $data_coords[0] : $maxlat; }
                if ($minlng === false) { $minlng = $data_coords[1]; } else { $minlng = ($data_coords[1] < $minlng) ? $data_coords[1] : $minlng; }
                if ($maxlng === false) { $maxlng = $data_coords[1]; } else { $maxlng = ($data_coords[1] > $maxlng) ? $data_coords[1] : $maxlng; }
            }
        }
        $lat = $maxlat - (($maxlat - $minlat) / 2);
        $lng = $maxlng - (($maxlng - $minlng) / 2);
        return $lat.','.$lng;
    }
    

    This returns the middle lat/lng coordinate for the center of a polygon.

    0 讨论(0)
提交回复
热议问题