Calculate the mid point of latitude and longitude co-ordinates

后端 未结 4 1545
我寻月下人不归
我寻月下人不归 2021-01-13 12:41

Does anyone know the best way to go about getting the mid-point of a pair of latitude and longitude points?

I mm using d3.js to draw points on a map and need to draw

4条回答
  •  猫巷女王i
    2021-01-13 12:57

    Check this question, you can use it to find the middle of your coordination on google map. I customized it to use with d3js. Hope this help you.

    In D3

    function midpoint (lat1, lng1, lat2, lng2) {
    
    lat1= deg2rad(lat1);
    lng1= deg2rad(lng1);
    lat2= deg2rad(lat2);
    lng2= deg2rad(lng2);
    
    dlng = lng2 - lng1;
    Bx = Math.cos(lat2) * Math.cos(dlng);
    By = Math.cos(lat2) * Math.sin(dlng);
    lat3 = Math.atan2( Math.sin(lat1)+Math.sin(lat2),
    Math.sqrt((Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By ));
    lng3 = lng1 + Math.atan2(By, (Math.cos(lat1) + Bx));
    
       return (lat3*180)/Math.PI .' '. (lng3*180)/Math.PI;
    }
    
    function deg2rad (degrees) {
        return degrees * Math.PI / 180;
    };
    

    Update 1

    If you try to draw curve line, you should create a path with coordination such as :

    var lat1=53.507651,lng1=10.046997,lat2=52.234528,lng2=10.695190;
    
    
        var svg=d3.select("body").append("svg").attr({width:700 , height:600});
        svg.append("path").attr("d", function (d) {
          var dC="M "+lat1+","+lng1+ " A " + midpoint (lat1, lng1, lat2, lng2)
             + " 0 1,0 " +lat2 +" , "+lng2 +"Z";
            return dC;
    
        })
        .style("fill","none").style("stroke" ,"steelblue");
    

    You need to create your curve line as you want in d3. JsFiddle here.

提交回复
热议问题