ThinkGeo.com    |     Documentation    |     Premium Support

Restricting zoom extent on map suite iOS for xamarin forms

Hi,
We found a nice sample for restricting zoom extent for the here restricting zoom extent which we would like to have in our iOS solution for xamarin, however there is no event args cancel possibility on the iOS api,

We also tried setting the restricted zoom extent using the following code which give me an impression that it would work but didn’t.

MapView.MapTools.ZoomMapTool.GlobeExtent = initialExtent;

This is our adopted code based on the restricting zoom extent in WPF edition also does not work:

private void MapViewOnCurrentExtentChanging(object sender, CurrentExtentChangingMapViewEventArgs e)
        {
            if (restrictedExtentForZoomOut == null)
            {
                return;
            }

            // While changing zoom level. Does not allow zooming out beyond the restricted extent by using the geometric
            // function IsWithin. The new extent has to be within the restricted extent or the zoomin out will be canceled.
            if (e.PreviousExtent.GetBoundingBox().Width != e.CurrentExent.GetBoundingBox().Width)
            {
                if (e.CurrentExent.IsWithin(restrictedExtentForZoomOut) == false)
                {
                    e.CurrentExent = e.PreviousExtent;
                }
            }
            else
            {
                // While panning. It checks for the difference in area of the overlap (GetIntersection) of the current extent and the
                //  new extent with the restricted extent for panning. That way, we can be outside the restricted extent for panning after
                //  zooming out and still pan to get more within the restricted extent for panning.
                RectangleShape currentIntersection = restrictedExtentForZoomOut.GetIntersection(e.PreviousExtent);
                RectangleShape newIntersection = restrictedExtentForZoomOut.GetIntersection(e.CurrentExent);
                //Gets the area. Meter is an arbitrary unit.
                double currentArea = currentIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
                double newArea = newIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
                if (newArea < currentArea)
                {
                    e.CurrentExent = e.PreviousExtent;
                }
            }
        }

Hi Syed,

I tested the code as below in WpfEdition and make that works, wish it’s helpful.

void Map1_CurrentExtentChanging(object sender, CurrentExtentChangingWpfMapEventArgs e)
{
if (restrictedExtentForZoomOut == null)
{
return;
}

        // While changing zoom level. Does not allow zooming out beyond the restricted extent by using the geometric
        // function IsWithin. The new extent has to be within the restricted extent or the zoomin out will be canceled.
        if (e.CurrentExtent.GetBoundingBox().Width != e.NewExtent.GetBoundingBox().Width)
        {
            if (e.NewExtent.IsWithin(restrictedExtentForZoomOut) == false)
            {
                e.Cancel = true;                    
            }
        }
        else
        {
            // While panning. It checks for the difference in area of the overlap (GetIntersection) of the current extent and the
            //  new extent with the restricted extent for panning. That way, we can be outside the restricted extent for panning after
            //  zooming out and still pan to get more within the restricted extent for panning.
            RectangleShape currentIntersection = restrictedExtentForZoomOut.GetIntersection(e.CurrentExtent);
            RectangleShape newIntersection = restrictedExtentForZoomOut.GetIntersection(e.NewExtent);
            //Gets the area. Meter is an arbitrary unit.
            double currentArea = currentIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
            double newArea = newIntersection.GetArea(GeographyUnit.Meter, AreaUnit.SquareMeters);
            if (newArea < currentArea)
            {
                e.Cancel = true;
            }
        }
    }

Regards,

Don

Dear Don,
Thank you for your reply. But I think you missed the requirement, it works for WPF. But we want to have this in MapSuite for iOS Xamarin forms. And the iOS API(xamarin) does not have this event cancel property. Hence we adopted the code a little(as I have written in previous entry).

Please help!

Regards,
Murshed

Hi Murshed,

It looks I misunderstand your requirement. I thought you cannot run this sample both under WPF edition and IOS edition, so I just help you solved the WPF part.

IOS guys are very busy and may cannot help us reply the question on IOS now. I am not sure whether it works, but I think you can try to set the original extent to Map.CurrentExtent instead of cancel.

Regards,

Don