amay077 / Xamarin.Forms.GoogleMaps

Map library for Xamarin.Forms using Google maps API
https://www.nuget.org/packages/Xamarin.Forms.GoogleMaps/
MIT License
545 stars 346 forks source link

Add ShowInfoWindow(pin) to Map class #745

Open chrisevans9629 opened 3 years ago

chrisevans9629 commented 3 years ago

SUMMARY

I would like the ability to show the info window pragmatically.

DETAILS

This could be useful when you want to select a pin and show it's info window without tapping on it. For example, if you have a list of items and want to go to that place on the map when that item is tapped.

// How to use of your requested feature
Map.ShowInfoWindow(pin)

PLATFORMS

Progress

I have already created a custom renderer that does exactly what I need, but it's not very clean as I don't have access to the internal classes. For example on UWP:

 public class CustomMapRenderer : MapRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Map> e)
        {
            if (e.OldElement is CustomMap map)
            {
                map.ShowLabelRequest -= Map_ShowLabelRequest;
            }
            if (e.NewElement is CustomMap map1)
            {
                map1.ShowLabelRequest += Map_ShowLabelRequest;
            }

            base.OnElementChanged(e);
        }

        StackPanel previous;
        private void Map_ShowLabelRequest(object sender, Xamarin.Forms.GoogleMaps.Pin e)
        {
            if (previous != null)
            {
                previous.Visibility = Visibility.Collapsed;
            }

            var item = e.NativeObject as ContentControl;

            var details = item.GetType().GetProperty("DetailsView").GetValue(item) as StackPanel;

            details.Visibility = Visibility.Visible;

            previous = details;
        }
    }