我有一个点列表(实际上是商店坐标),我需要确定它们是否位于某些边界内。
在C#中,我知道如何从lat&lng创建一个点
var point = new GeoCoordinate(latitude, longitude);
但是如何检查该点是否包含在由其他两点定义的矩形中:
var swPoint = new GeoCoordinate(bounds.swlat, bounds.swlng);
var nePoint = new GeoCoordinate(bounds.nelat, bounds.nelng);
我可以使用任何课程方法吗?
如果你正在使用
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
}
有一个角落需要考虑。如果由sw,ne定义的框穿过180度经度,则上述方法将失败。因此,必须编写额外的代码来覆盖该情况,从而降低该方法的性能。