1

我开发了一个 xamarin 表单应用程序,我在数据库中存储了一些位置。我使用具有纬度和经度的 efcore 存储 Location NetTopology 套件。然后我可以从地图上的某个点找到最近的地方。

但是,如果我移动地图或缩小地图,我如何才能找到存储在我的数据库中的新区域中的地点以将它们固定在地图上?

有例子吗?

我真的很难找到一种方法来说明我数据库中的这个地点列表是地图显示的一部分。

我使用 xamarin 地图。

谢谢

4

2 回答 2

5

为此,您需要找到地图可见区域的边界。让我解释一下。

绑定类:

 public class Bounds
    {
        public double South { get; set; }
        public double West { get; set; }
        public double North { get; set; }
        public double East { get; set; }
    }

地图有一个称为可见区域的属性。当地图的属性发生变化时,地图的可见区域也会发生变化。下面是地图类:

Bounds bounds = new Bounds();
customMap.PropertyChanged += (sender, e) =>
                {
                    Debug.WriteLine(e.PropertyName + " just changed!");
                    if (e.PropertyName == "VisibleRegion" && customMap.VisibleRegion != null)
                        CalculateBoundingCoordinates(customMap.VisibleRegion);
                };

static void CalculateBoundingCoordinates(MapSpan region)
        {
            try
            {
                _region = region;
                var center = region.Center;
                var halfheightDegrees = region.LatitudeDegrees / 2;
                var halfwidthDegrees = region.LongitudeDegrees / 2;

        var left = center.Longitude - halfwidthDegrees;
        var right = center.Longitude + halfwidthDegrees;
            var top = center.Latitude + halfheightDegrees;
            var bottom  = center.Latitude - halfheightDegrees;

            if (left < -180) left = 180 + (180 + left);
            if (right > 180) right = (right - 180) - 180;

        bounds.West = left;
        bounds.East = right;
        bounds.North = top;
        bounds.South = bottom;
    }
}

现在使用这些边界查询您的数据库。

于 2020-02-26T06:07:37.540 回答
0

我假设您已将 lat, long 存储在 db 中,用于您想要在地图上显示的区域。所以你可以像这样查询你的数据库:

var GetPlace = "Select Id, AreaName, Lat, Long From TableName Where Lat Between East And West And Long Between North And South";

这里的北、南、东、西是我们从上述方法得到的边界。

于 2020-03-02T06:15:40.197 回答