How to get the current zoom level in Xamarin.Forms.GoogleMaps?

stesvisstesvis USMember ✭✭✭

Is it possible to get the current zoom level in Xamarin.Forms.GoogleMaps?
I need to fire an event when users zoom in or out..

Thanks

Best Answer

  • stesvisstesvis US ✭✭✭
    Accepted Answer

    Thanks @LandLu I eventually went another route.
    I used the CameraIdled event, and then the argument is a CameraPosition, which has a zoom property.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    Firstly, create a custom map in Forms:

    public class CustomMap : Map
    {
        public Func<float> GetZoom;
    }
    

    Secondly, define the corresponding renderer for this map in Android:

    [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapViewRenderer))]
    namespace WorkingWithMaps.Android
    {
        public class CustomMapViewRenderer : MapRenderer
        {
            public CustomMapViewRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    var FormsMap = e.NewElement as CustomMap;
                    FormsMap.GetZoom = () => myMap.CameraPosition.Zoom;
                }
            }
    
            GoogleMap myMap;
            protected override void OnMapReady(GoogleMap map)
            {
                base.OnMapReady(map);
    
                myMap = map;
            }
        }
    }
    

    At last, you could obtain the zoom level in the forms like:

    var button = new Button { Text = "Click" };
    button.Clicked += (sender, e) =>
    {
        var zoomLevel = map.GetZoom?.Invoke();
    };
    stack.Children.Add(button);
    
  • stesvisstesvis USMember ✭✭✭
    Accepted Answer

    Thanks @LandLu I eventually went another route.
    I used the CameraIdled event, and then the argument is a CameraPosition, which has a zoom property.

Sign In or Register to comment.