xamarin.formsentity-framework-coremapsnettopologysuite

Xamarin forms get places in the map area


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


Solution

  • 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.