maui icon indicating copy to clipboard operation
maui copied to clipboard

Provide a way to show pin hint window programmatically

Open smitha-cgi opened this issue 2 years ago • 3 comments

Description

Map pins have the ability to show an information window when clicked. Please provide a way to show this information window programmatically.

Public API Changes


Pin pin = new()
{
    Label = "some label"
    Address = "123 A Street"
    Type = PinType.Place,
    Location = new Location(-36, 144);
};

map.Pins.Add(pin);

pin.ShowInformationWindow(); //New method to be added

Intended Use-Case

I am showing a map with lots of pins representing particular items of interest. When a user selects a name from a drop down list, if that location has a pin present I want to highlight it by showing the information window , i.e. as if the user had clicked on it. There does not seem to be any way to do this at the moment.

smitha-cgi avatar Oct 11 '22 03:10 smitha-cgi

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Oct 11 '22 18:10 ghost

+1 this is very important functionality Maybe via triggering its focused /selected state programmatically?

janseris avatar Oct 11 '22 20:10 janseris

I will take this issue.

Miro382 avatar Oct 13 '22 17:10 Miro382

@janseris A hack workaround for this is on Android to create a pin which sits directly on top of an existing pin using the Google map Marker class. Haven't looked into the iOS side of things yet but it'll have something to do with the SelectAnnotation method on the MKMapView class.

var mapHandler = (IMapHandler?)map.Handler;
var googleMap = mapHandler?.Map;
if (mapHandler is not null && googleMap is not null)
{
    MarkerOptions options = new();
    options.SetPosition(new LatLng(pin.Location.Latitude, pin.Location.Longitude));
    options.SetTitle(pin.Label);
    options.SetSnippet(pin.Address);                            
    
    Marker marker = googleMap.AddMarker(options);
    marker.ShowInfoWindow();
}

smitha-cgi avatar Oct 16 '22 22:10 smitha-cgi

Providing search phrases for this issue, because search engines find nothing helpful: How to select a pin with code/c# for net Maui map. Changing the Selected pin.

I'm trying to accomplish this now as well... scenario: synchronized map and collectionview. Each location shown in collectionview has a corresponding pin on map.

rrelyea avatar Apr 11 '24 15:04 rrelyea

I will take this issue.

Did you solve it?

Lug16 avatar May 02 '24 17:05 Lug16

Ideally, these things are needed for my scenario:

  • Having a Map.PinSelectionChanged event (naming?)
  • having the ability to change the selected pin for the map: Map.SelectedPin property?
  • Having the ability to ensure a pin is visible, as it may be hidden due to other nearby pins. Minimally, if I programmatically selected a hidden pin, I'd imagine that it should be shown, hiding neighboring pins if necessary (??)

Happy to try out early bits or help in any way.

rrelyea avatar May 02 '24 18:05 rrelyea

We are looking at doing something like this but I have not tested yet for iOS.

private static Marker AddMarker(GoogleMap map, IMapPin pin, List<Marker> markers, MarkerOptions markerOption)
    {
        Marker marker = map.AddMarker(markerOption);
        pin.MarkerId = marker.Id;
        markers.Add(marker);

        return marker;
    }

private void addPins(IEnumerable<IMapPin> mapPins)
    {
        if (Map is null || MauiContext is null)
        {
            return;
        }

        foreach (IMapPin pin in mapPins)
        {
            IElementHandler pinHandler = pin.ToHandler(MauiContext);
            if (pinHandler is IMapPinHandler mapPinHandler)
            {
                if (pin is CustomStorePin customStorePin)
                {
#if ANDROID
                MarkerOptions markerOption = mapPinHandler.PlatformView;

                    customStorePin.Marker = AddMarker(Map, pin, Markers, markerOption);

#elif IOS

                    customStorePin.DisplayAnnotation = () =>
                    {
                        if (mapPinHandler.PlatformView as MKMapView is MKMapView mKMapView && mKMapView.Annotations.SingleOrDefault(annotation => annotation.Coordinate.Latitude == pin?.Location.Latitude && annotation.Coordinate.Longitude == pin.Location.Longitude) is IMKAnnotation mKAnnotation)
                        {
                            mKMapView.SelectAnnotation(mKAnnotation, true);
                        }
                    };
#endif
                }
            }
        }
    }

For iOS DisplayAnnotation is just an action on your VM that will call SelectAnnotation @smitha-cgi was alluding to.

LeoJHarris avatar Jun 06 '24 05:06 LeoJHarris