我怎么知道Lat,Lng点是否包含在圆圈内?

Stu*_*ard 14 geometry curve google-maps point latitude-longitude

好吧非常自我解释.我正在使用谷歌地图,我试图找出一个纬度,长点是否在半径为x的圆内(x由用户选择).

边界框不适用于此.我已经尝试使用以下代码:

distlatLng = new google.maps.LatLng(dist.latlng[0],dist.latlng[1]);
var latLngBounds = circle.getBounds();
if(latLngBounds.contains(distlatLng)){
      dropPins(distlatLng,dist.f_addr);
}
Run Code Online (Sandbox Code Playgroud)

这仍然会导致标记位于圆圈之外.

我猜这是一些简单的数学需要计算曲率或面积,但我不知道从哪里开始.有什么建议?

kai*_*ser 16

带可拖动中心标记的工作解决方案

你有没有试过contains?看一下LatLngBounds构造函数.

我写了一篇关于它的文章,其中包含一个工作JSFiddle.net示例的链接.

jsFiddle的屏幕截图


更新版本.


Gra*_*her 10

不幸的是,毕达哥拉斯在球体上没有任何帮助.因此Stuart Beard的回答是不正确的; 经度差异与米数没有固定的比率,但取决于纬度.

正确的方法是使用公式来获得很大的圆距离.假设球形地球,这是一个很好的近似(在C++中):

/** Find the great-circle distance in metres, assuming a spherical earth, between two lat-long points in degrees. */
inline double GreatCircleDistanceInMeters(double aLong1,double aLat1,double aLong2,double aLat2)
    {
    aLong1 *= KDegreesToRadiansDouble;
    aLat1 *= KDegreesToRadiansDouble;
    aLong2 *= KDegreesToRadiansDouble;
    aLat2 *= KDegreesToRadiansDouble;
    double cos_angle = sin(aLat1) * sin(aLat2) + cos(aLat1) * cos(aLat2) * cos(aLong2 - aLong1);

    /*
    Inaccurate trig functions can cause cos_angle to be a tiny amount
    greater than 1 if the two positions are very close. That in turn causes
    acos to give a domain error and return the special floating point value
    -1.#IND000000000000, meaning 'indefinite'. Observed on VS2008 on 64-bit Windows.
    */
    if (cos_angle >= 1)
        return 0;

    double angle = acos(cos_angle);
    return angle * KEquatorialRadiusInMetres;
    }
Run Code Online (Sandbox Code Playgroud)

哪里

const double KPiDouble = 3.141592654;
const double KDegreesToRadiansDouble = KPiDouble / 180.0;
Run Code Online (Sandbox Code Playgroud)

/**
A constant to convert radians to metres for the Mercator and other projections.
It is the semi-major axis (equatorial radius) used by the WGS 84 datum (see http://en.wikipedia.org/wiki/WGS84).
*/
const int32 KEquatorialRadiusInMetres = 6378137;
Run Code Online (Sandbox Code Playgroud)

  • 你仍然选择了极地附近危险错误的答案,@ StuartBeard,我在适当的时候都是近似值,但我认为你选择非笛卡尔空间中的笛卡尔距离作为"答案"是不负责任的. (2认同)

zav*_*ych 5

使用Google Maps API几何库计算圆的中心与标记之间的距离,然后将其与半径进行比较.

var pointIsInsideCircle = google.maps.geometry.spherical.computeDistanceBetween(circle.getCenter(), point) <= circle.getRadius();
Run Code Online (Sandbox Code Playgroud)


Ash*_*osh 5

这很简单.您只需计算中心与给定点之间的距离,并将其与半径进行比较.您可以从此处获取帮助以计算两个拉特朗之间的距离


Stu*_*ard 1

我确实有点傻了。思考一下我们可以利用毕达哥拉斯定理。

我们有距某个点的最大距离(X 英里)、两个纬度和两个经度。如果我们用这些形成一个三角形,那么我们就可以求解到该点的距离。

所以说我们知道point1坐标lat1,lng1是圆的中心,point2坐标lat2,lng2是我们试图决定是否在圆中的点。

point1我们使用由和确定的点形成一个直角三角形point2。这point3将有坐标lat1,lng2lat2,lng1(哪个并不重要)。然后我们计算差异(或者如果您愿意)距离 -latDiff = lat2-lat1以及lngDiff = lng2-lng1

然后我们使用 Pythagorus - 计算距中心的距离dist=sqrt(lngDiff^2+latDiff^2)

我们必须将所有内容转换为米,以便它能够在 google 地图上正常工作,因此英里乘以 1609(大约),纬度/经度乘以 111000(大约)。这并不完全准确,但它做得足够了。

希望一切都有意义。

  • 聚会有点晚了,但是您是否考虑过所使用的地图投影会扭曲事物的形状和距离这一事实? (2认同)
  • 这完全忽略了地图投影。纬度和经度间隔不相等!它在北纬或南纬地区根本不起作用。仅在赤道附近。这是不对的。 (2认同)