2

I develop a xamarin forms app where I store some places in my database. I store Location NetTopology suite with efcore with latitude and longitude. Then I can find the closest places from a point on the map.

But if i move the map or zoom out how can I find the places in the new area which are stored in my database to pin them on the map?

Is there an example?

I really struggle to find a way to say this list of places in my database are part of what the map is showing.

I use xamarin maps.

Thanks

dalton5
  • 915
  • 2
  • 14
  • 28
  • When you zoom out, CameraUpdated event fires, you should get the new bounds of the map and check which points are inside it – memsranga Feb 26 '20 at 02:34
  • when the map moves, just use the center point of the new visible region and query your db for nearby places. – Jason Feb 26 '20 at 03:09

3 Answers3

6

for doing this you need to find bounds of map's visible region. let me explain this.

Bound class:

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

Map has a property called visible region. when map's property will change, map's visible region will also change. Below is the map class:

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;
    }
}

Now query your database with these bounds.

Harsh Shah
  • 188
  • 2
  • 6
  • How can I query with NetTopology with 4 coordinates? I can do it for Distance between. But for one area how can I perform it? – dalton5 Mar 01 '20 at 23:26
0

I assume that you have lat, long stored in db for area which you want to show on map. so you can query your db like this:

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

Here North, south, East and West are the Bounds which we get from above method.

Harsh Shah
  • 188
  • 2
  • 6
0

I had the same question and the answer is great but I had a problem with the query, because a few times in the coordinates there are negative values.

In case you have negative latitude or longitude your query should be like this:

    SELECT "Column Name"
    FROM "Table Name"
    WHERE Longitude >= West 
      AND Longitude <= East
      AND Latitude >= South
      AND Latitude <= North

In case someone had the same problem

lepsch
  • 8,927
  • 5
  • 24
  • 44