如何查找GeoCoordinate点是否在边界内

Max*_*lli 11 c# maps coordinates

我有一个点列表(实际上是商店坐标),我需要确定它们是否位于某些边界内.

在C#中我知道如何从lat&lng创建一个点

var point = new GeoCoordinate(latitude, longitude);
Run Code Online (Sandbox Code Playgroud)

但是,如何检查该点是否包含在由其他两点定义的矩形中:

    var swPoint = new GeoCoordinate(bounds.swlat, bounds.swlng);
    var nePoint = new GeoCoordinate(bounds.nelat, bounds.nelng);
Run Code Online (Sandbox Code Playgroud)

我可以使用任何课程方法吗?

phi*_*gon 12

如果您使用的是 http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate.aspx

您必须编写自己的方法来进行此检查.您可能希望将其作为扩展方法(在线扩展方法可用的资源数量.)

然后它几乎一样简单

public static Boolean isWithin(this GeoCoordinate pt, GeoCoordinate sw, GeoCoordinate ne)
{
   return pt.Latitude >= sw.Latitude &&
          pt.Latitude <= ne.Latitude &&
          pt.Longitude >= sw.Longitude &&
          pt.Longitude <= ne.Longitude
}
Run Code Online (Sandbox Code Playgroud)

有一个角落需要考虑.如果sw,ne定义的框穿过180度经度,则上述方法将失败.因此,必须编写额外的代码来覆盖该情况,从而降低该方法的性能.

  • 这是在大海洋中拥有180度线的一个优势.那里的商店很少. (3认同)
  • 对于给定的解决方案,实际上越过赤道不是问题,因为在赤道以南的纬度只是负数。仅越过180度经线需要更多代码。 (2认同)