如何以xamarin形式查看两个引脚之间的距离并将距离作为一个字符串?

med*_*edo 2 c# xamarin xamarin.forms

我在地图上有两个长度和纬度的针脚,我希望看到它们之间的距离.这就是我到目前为止所拥有的.

        var myPin = new Pin ();
        myPin.Label = "pin1";
        myPin.Address = "test1";
        myPin.Position = new Position(37.797513, -122.402068);

        myPin.Label = "pin2";
        myPin.Address = "test2";
        myPin.Position = new Position(37.7, -122.3);

        mymap.Pins.Add(myPin);
Run Code Online (Sandbox Code Playgroud)

所以我的地图上有两个不同的针脚.我怎样才能看到这两个公里之间的距离?我的目标是制作一个带有距离(km)的标签作为文本.

(我有一个客户渲染器基础准备好了解决距离功能的唯一方法是通过渲染器).

sak*_*mar 8

如果你有两个引脚,你有它们的经度和纬度值,那么可以使用下面的代码计算em之间的距离(取自geodatasource.com)

private double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
  double theta = lon1 - lon2;
  double dist = Math.Sin(deg2rad(lat1)) * Math.Sin(deg2rad(lat2)) + Math.Cos(deg2rad(lat1)) * Math.Cos(deg2rad(lat2)) * Math.Cos(deg2rad(theta));
  dist = Math.Acos(dist);
  dist = rad2deg(dist);
  dist = dist * 60 * 1.1515;
  if (unit == 'K') {
    dist = dist * 1.609344;
  } else if (unit == 'N') {
    dist = dist * 0.8684;
    }
  return (dist);
}

private double deg2rad(double deg) {
  return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
  return (rad / Math.PI * 180.0);
}
Run Code Online (Sandbox Code Playgroud)

所以基本上你只需要一个lat/long值的起始参考,你可以参考其他引脚来显示这个距离作为标签