简体   繁体   English

找出矩形中的长/纬度

[英]find out if long/lat within rectangle

Given a top-left long/lat and a bottom-right long/lat how can i find out if a given long/lat falls within the rectangle ? 给定一个左上角的长/纬度和一个右下角的长/纬度,如何确定给定的长/纬度是否落在矩形内?

Ideally i would be looking at something like 理想情况下,我会看到类似的东西

bool IsWithinArea(float topLeftLat,float topLeftLong,
   float bottomRightLat,float bottomRightLong,float testLat,float testLong)

Update 更新

The one problem is that the rectangle created from the long/lat may be from a rotated map, so bottom right will not always be greater than top left... 一个问题是从long / lat创建的矩形可能来自旋转的地图,因此右下角并不总是大于左上角...

We can make it more interesting than trivial checks: 我们可以比琐碎的检查更有趣:

return new Rect(topLeftLat, topLeftLong, bottomRightLat - topLeftLat, bottomRightLong - topLeftLong)
      .Contains(testLat, testLong);

PS: Rect.Contains(...) method PS: Rect.Contains(...)方法

Not sure if I'm thinking to simple 不确定我是否想要简单

bool IsWithinArea(float topLeftLat, float topLeftLong, float bottomRightLat, float bottomRightLong, float testLat, float testLong)
{
    return (testLat >= topLeftLat && testLat <= bottomRightLat && testLong >= topLeftLong && testLong <= bottomRightLong);
}

Assuming that Lat is the x coordinate und Long is the y coordinate and also assuming that the coordinate systems has its origin at the left top: 假设Lat是x坐标,而long是y坐标,并且假设坐标系的原点位于左上角:

public bool IsWithinArea(float topLeftLat,float topLeftLong,
       float bottomRightLat,float bottomRightLong,float testLat,float testLong) {

          return (testLat >= topLeftLat && testLat <= bottomRightLat && testLong >= topLeftLong && testLong <= bottomRightLong);

    }

One approach would be to normalize your long/lat pairs to simple x/y coordinates. 一种方法是将long / lat对标准化为简单的x / y坐标。 Then it should be a simple excersize to determine if a point falls within the rectangle. 那么它应该是一个简单的excersize来确定一个点是否落在矩形内。

long/lat to x/y conversion can be found here: Convert Lat/Longs to X/Y Co-ordinates 可以在此处找到long / lat到x / y转换: 将Lat / Longs转换为X / Y坐标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM