简体   繁体   English

C#/ WinRT中两个位置之间的距离和其他内容

[英]Distance and other stuff between two Location in C# / WinRT

How can I get the distance between two Location in C#? 如何获取C#中两个Location之间的距离? It also would be very nice to get the direction between two Locations if I have the compass value of the device. 如果我具有设备的指南针值,那么获得两个位置之间的方向也将非常好。

is there any api for this? 有没有这个API?

It depends. 这取决于。 What do you mean by Location ? 您的位置是什么意思?

Case 1: When location is geographic point (Latitude, Longitude) 情况1:位置为地理位置(纬度,经度)时

You need to go through spherical geometry. 您需要检查球形几何体。 In fact you should make some calculation on a sphere (or Ellipsoid or other Spheroids). 实际上,您应该对球体(或椭球体或其他球体)进行一些计算。 But the best way to avoid doing such complicated calculation is to use Microsoft.SqlServer.Types.dll (v.10 or v.11). 但是,避免进行此类复杂计算的最佳方法是使用Microsoft.SqlServer.Types.dll (v.10或v.11)。 If you have SQL Server 2012 (or 2008) installed on your system, you can find it somewhere like this (otherwise you may download it from the web): 如果您的系统上安装了SQL Server 2012(或2008),则可以在以下位置找到它(否则可以从Web上下载它):

"C:\Program Files\Microsoft SQL Server\110\Shared\Microsoft.SqlServer.Types.dll"

Then using this dll you can declare two SqlGeography type and call the STDistance() method and everything will be done correctly. 然后,使用此dll可以声明两个SqlGeography类型并调用STDistance()方法,一切将正确完成。 (Remember there are lost of issue here, I'm not going to complicate it more than this). (请记住,这里丢失了问题,我将不会使问题更加复杂)。 Here is the code: 这是代码:

SqlGeography p1 =
SqlGeography.STPointFromText(new System.Data.SqlTypes.SqlChars("POINT( lat1, long1)"), 4326); // SRID of WGS84: 4326

SqlGeography p2 =
SqlGeography.STPointFromText(new System.Data.SqlTypes.SqlChars("POINT( lat2, long2)"), 4326); // SRID of WGS84: 4326

double distance = p1.STDistance(p2).Value;

Case 2: When location is a simple local 2D point (x, y) on a plane 情况2:位置是平面上的简单局部2D点(x,y)

In this case you can use the famous formula to calculate the distance: 在这种情况下,您可以使用著名的公式来计算距离:

double distance = Math.Sqrt(dx*dx + dy*dy); //dx and dy are the difference of the x and y of the two points

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

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