Skip to content

Commit

Permalink
Avoid propagate Map tap event tapping a Pin on iOS (#11582) Fixes #11532
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Nov 23, 2022
1 parent 9237735 commit 2921206
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Maui.Controls.Sample.Pages.MapsGalleries.MapPinsGallery"
Title="Map Pins">

<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://schemas.microsoft.com/dotnet/2021/maui/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="Maui.Controls.Sample.Pages.MapsGalleries.MapPinsGallery"
Title="Map Pins">
<Grid RowDefinitions="Auto, *">
<HorizontalStackLayout Grid.Row="0">
<Button Text="Add Pin" Clicked="AddPin_Clicked" />
<Button Text="Remove Pin" Clicked="RemovePin_Clicked" />
<Button Text="Add 10 Pins" Clicked="Add10Pins_Clicked" />
<HorizontalStackLayout
Grid.Row="0">
<Button
Text="Add Pin"
Clicked="OnAddPinClicked" />
<Button
Text="Remove Pin"
Clicked="OnRemovePinClicked" />
<Button
Text="Add 10 Pins"
Clicked="OnAdd10PinsClicked" />
</HorizontalStackLayout>
<Map x:Name="pinsMap" Grid.Row="1" />
<Map
x:Name="pinsMap"
Grid.Row="1"
MapClicked="OnMapClicked" />
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Maui.Controls.Sample.Pages.MapsGalleries
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPinsGallery
{
private readonly Random _locationRandomSeed = new();
private int _locationIncrement = 0;
readonly Random _locationRandomSeed = new();
int _locationIncrement = 0;

// TODO generate actual random pins
private readonly Position[] _randomLocations =
Expand Down Expand Up @@ -62,26 +62,26 @@ public MapPinsGallery()
Location = new Position(47.64232, -122.13684),
};

microsoftPin.MarkerClicked += (s, a) =>
microsoftPin.MarkerClicked += (sender, args) =>
{
DisplayAlert("Marker", "OK", "OK");
DisplayAlert("Marker", $"Marker Clicked: {((Pin)sender).Label}", "OK");
};

// TODO this doesn't seem to work on iOS?
microsoftPin.InfoWindowClicked += (s, a) =>
microsoftPin.InfoWindowClicked += (sender, args) =>
{
DisplayAlert("Info", "OK", "OK");
DisplayAlert("Info", $"Info Window Clicked: {((Pin)sender).Label}", "OK");
};

pinsMap.Pins.Add(microsoftPin);
}

private void AddPin_Clicked(object sender, EventArgs e)
void OnAddPinClicked(object sender, EventArgs e)
{
AddPin();
}

private void RemovePin_Clicked(object sender, EventArgs e)
void OnRemovePinClicked(object sender, EventArgs e)
{
if (pinsMap.Pins.Count > 0)
{
Expand All @@ -90,21 +90,26 @@ private void RemovePin_Clicked(object sender, EventArgs e)
}
}

private void Add10Pins_Clicked(object sender, EventArgs e)
void OnAdd10PinsClicked(object sender, EventArgs e)
{
for (int i = 0; i <= 10; i++)
{
AddPin();
}
}

private void AddPin()
void AddPin()
{
pinsMap.Pins.Add(new Pin()
{
Label = $"Location {_locationIncrement++}",
Location = _randomLocations[_locationRandomSeed.Next(0, _randomLocations.Length)],
});
}

void OnMapClicked(object sender, MapClickedEventArgs e)
{
DisplayAlert("Map", $"Map {e.Location.Latitude}, {e.Location.Longitude} clicked.", "Ok");
}
}
}
18 changes: 17 additions & 1 deletion src/Core/maps/src/Platform/iOS/MauiMKMapView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,11 @@ void Startup()
{
RegionChanged += MkMapViewOnRegionChanged;
DidSelectAnnotationView += MkMapViewOnAnnotationViewSelected;
AddGestureRecognizer(_mapClickedGestureRecognizer = new UITapGestureRecognizer(OnMapClicked));

AddGestureRecognizer(_mapClickedGestureRecognizer = new UITapGestureRecognizer(OnMapClicked)
{
ShouldReceiveTouch = OnShouldReceiveMapTouch
});
}

void Cleanup()
Expand All @@ -194,9 +198,11 @@ void MkMapViewOnAnnotationViewSelected(object? sender, MKAnnotationViewEventArgs

if (pin == null)
return;

// SendMarkerClick() returns the value of PinClickedEventArgs.HideInfoWindow
// Hide the info window by deselecting the annotation
bool deselect = pin.SendMarkerClick();

if (deselect)
DeselectAnnotation(annotation, false);
}
Expand Down Expand Up @@ -292,12 +298,22 @@ void OnCalloutClicked(IMKAnnotation annotation)
return mapElement?.ToHandler(handler?.MauiContext!).PlatformView as T;
}

bool OnShouldReceiveMapTouch(UIGestureRecognizer recognizer, UITouch touch)
{
if (touch.View is MKAnnotationView)
return false;

return true;
}

static void OnMapClicked(UITapGestureRecognizer recognizer)
{
if (recognizer.View is not MauiMKMapView mauiMkMapView)
return;

var tapPoint = recognizer.LocationInView(mauiMkMapView);
var tapGPS = mauiMkMapView.ConvertPoint(tapPoint, mauiMkMapView);

if (mauiMkMapView._handlerRef.TryGetTarget(out IMapHandler? handler))
handler?.VirtualView.Clicked(new Devices.Sensors.Location(tapGPS.Latitude, tapGPS.Longitude));
}
Expand Down

0 comments on commit 2921206

Please sign in to comment.