谷歌地图两个圆圈交叉点

时间:2014-03-18 03:01:43

标签: javascript algorithm google-maps google-maps-api-3 geometry

是否有一种简单的方法可以获得circles中两个Google Maps API V3的交叉点(如果有)的lat / lng?或者我应该采用hard方式?

编辑:在我的问题中,圆圈的半径始终相同,以免使解决方案更容易。

3 个答案:

答案 0 :(得分:11)

是的,对于同等的圈子,可以详细说明解决方案:
设第一个圆心为A点,第二个圆心为F,中点为C,交点为B,D。 ABC是直角C的直角球面三角形。

enter image description here

我们想要找到角度A - 这是从A-F方向的偏离角度。球面三角学(纳皮尔对右球形三角形的规则)给出了公式:

cos(A)= tg(AC) * ctg(AB) 其中一个符号表示球面角度,双符号表示大圆弧。角度(AB,AC)。我们可以看到AB =圆半径(当然是弧度),AC =大圆弧上A和F之间的半距离。 要查找AC(和其他值),我将使用代码from this excellent page

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 

和我们的

AC = c/2

如果给出的圆半径Rd是千米,那么

AB = Rd / R = Rd / 6371

现在我们可以找到角度

A = arccos(tg(AC) * ctg(AB))

开始轴承(AF方向):

var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
        Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
var brng = Math.atan2(y, x);

交叉点'轴承:

B_bearing = brng - A
D_bearing = brng + A

交叉点'坐标:

var latB = Math.asin( Math.sin(lat1)*Math.cos(Rd/R) + 
              Math.cos(lat1)*Math.sin(Rd/R)*Math.cos(B_bearing) );
var lonB = lon1.toRad() + Math.atan2(Math.sin(B_bearing)*Math.sin(Rd/R)*Math.cos(lat1), 
                     Math.cos(Rd/R)-Math.sin(lat1)*Math.sin(lat2));

和D_bearing相同

latB,lonB以弧度为单位

答案 1 :(得分:4)

对于r1 = r2 =:r,可以简化computation the "hard" way。我们首先必须将圆心P1,P2从(lat,lng)转换为笛卡尔坐标(x,y,z)。

var DEG2RAD = Math.PI/180;
function LatLng2Cartesian(lat_deg,lng_deg)
{
  var lat_rad = lat_deg*DEG2RAD;
  var lng_rad = lng_deg*DEG2RAD;
  var cos_lat = Math.cos(lat_rad);
  return {x: Math.cos(lng_rad)*cos_lat,
          y: Math.sin(lng_rad)*cos_lat,
          z: Math.sin(lat_rad)};
}

var P1 = LatLng2Cartesian(lat1, lng1);
var P2 = LatLng2Cartesian(lat2, lng2);

但是可以更容易地计算保持圆的平面的交线。设d为实际圆心(在平面内)与表面上对应点P1或P2的距离。一个简单的推导显示(用R表示地球的半径):

var R = 6371; // earth radius in km
var r = 100; // the direct distance (in km) of the given points to the intersections points
// if the value rs for the distance along the surface is known, it has to be converted:
// var r = 2*R*Math.sin(rs/(2*R*Math.PI));
var d = r*r/(2*R);

现在让S1和S2成为交叉点,将S作为中点。使用s = |OS|t = |SS1| = |SS2|(其中O =(0,0,0)是地球的中心),我们可以从简单的推导中获得:

var a = Math.acos(P1.x*P2.x + P1.y*P2.y + P1.z*P2.z); // the angle P1OP2
var s = (R-d)/Math.cos(a/2);
var t = Math.sqrt(R*R - s*s);

现在,自r1 = r2起,点S,S1,S2位于P1和P2之间的中间平面。对于v_s = OS,我们得到:

function vecLen(v)
{ return Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z); }
function vecScale(scale,v)
{ return {x: scale*v.x, y: scale*v.y, z: scale*v.z}; }

var v = {x: P1.x+P2.x, y: P1.y+P2.y, z:P1.z+P2.z}; // P1+P2 is in the middle of OP1 and OP2
var S = vecScale(s/vecLen(v), v);

function crossProd(v1,v2)
{
  return {x: v1.y*v2.z - v1.z*v2.y,
          y: v1.z*v2.x - v1.x*v2.z,
          z: v1.x*v2.y - v1.y*v2.x};
}
var n = crossProd(P1,P2); // normal vector to plane OP1P2 = vector along S1S2
var SS1 = vecScale(t/vecLen(n),n);

var S1 = {x: S.x+SS1.x, y: S.y+SS1.y, z: S.z+SS1.z}; // S + SS1
var S2 = {x: S.x-SS1.x, y: S.y-SS2.y, z: S.z-SS1.z}; // S - SS1

最后我们必须转换回(lat,lng):

function Cartesian2LatLng(P)
{
  var P_xy = {x: P.x, y:P.y, z:0}
  return {lat: Math.atan2(P.y,P.x)/DEG2RAD, lng: Math.atan2(P.z,vecLen(P_xy))/DEG2RAD};
}
var S1_latlng = Cartesian2LatLng(S1);
var S2_latlng = Cartesian2LatLng(S2);

答案 2 :(得分:4)

Yazanpro,对此迟到的回应感到抱歉。

您可能对MBo方法的简洁变体感兴趣,该方法在两个方面进行了简化:

  • 首先利用google.maps API的一些内置功能来避免大量的数学运算。
  • 其次,使用2D模型计算夹角,代替MBo的球形模型。我最初不确定这种简化的有效性,但我对a fork of MBo's fiddle中的测试表示满意,即除了与地球大小相关的最大圆圈(例如在低缩放级别),误差很小。< / LI>

这是功能:

function getIntersections(circleA, circleB) {
    /* 
     * Find the points of intersection of two google maps circles or equal radius
     * circleA: a google.maps.Circle object 
     * circleB: a google.maps.Circle object
     * returns: null if 
     *    the two radii are not equal 
     *    the two circles are coincident
     *    the two circles don't intersect
     * otherwise returns: array containing the two points of intersection of circleA and circleB
     */

    var R, centerA, centerB, D, h, h_;

    try {

        R = circleA.getRadius();
        centerA = circleA.getCenter();
        centerB = circleB.getCenter();

        if(R !== circleB.getRadius()) {
            throw( new Error("Radii are not equal.") );
        }
        if(centerA.equals(centerB)) {
            throw( new Error("Circle centres are coincident.") );
        }

        D = google.maps.geometry.spherical.computeDistanceBetween(centerA, centerB); //Distance between the two centres (in meters)

        // Check that the two circles intersect
        if(D > (2 * R)) {
            throw( new Error("Circles do not intersect.") );
        }

        h = google.maps.geometry.spherical.computeHeading(centerA, centerB); //Heading from centre of circle A to centre of circle B. (in degrees)
        h_ = Math.acos(D / 2 / R) * 180 / Math.PI; //Included angle between the intersections (for either of the two circles) (in degrees). This is trivial only because the two radii are equal.

        //Return an array containing the two points of intersection as google.maps.latLng objects
        return [
            google.maps.geometry.spherical.computeOffset(centerA, R, h + h_),
            google.maps.geometry.spherical.computeOffset(centerA, R, h - h_)
        ];
    }
    catch(e) {
        console.error("getIntersections() :: " + e.message);
        return null;
    }
}

顺便说一下,不要不尊重MBo - 这是一个很好的答案。