My ViewModel created a list of pins that should be mapped. In the OnAppearing code for my page, I am adding code for the MarkerClicked event as follows:
protected override void OnAppearing()
{
try
{
base.OnAppearing();
// Add the pin click events
foreach (var pin in _MapPlace.PinsToMap)
{
pin.MarkerClicked += async (s, args) =>
{
args.HideInfoWindow = true;
string pinName = ((Pin)s).Label;
await DisplayAlert("Pin Clicked", $"{pinName} was clicked.", "Ok");
};
}
BindingContext = _MapPlace;
_MapPlace.MoveMap();
MapPlaceList.PlaceList = _MapPlace.MasterPlaces;
}
catch (Exception ex)
{
App.ProcessException(ex);
}
_MasterPage.IsBusy = false;
}
When I run the app (on Android) and I click on a pin - the normal pin details show (i.e. the Title and the Addresss) but the MarkerClick code never appears to execute.
Here is the code that populates the pin list in my VM.
private async Task UpdatePinList()
{
try
{
//Linq Query to get the places to map
var workPinList = (from p in MasterPlaces
where p.Selected == true
select new Pin()
{
Label = p.Title,
Address = p.Address,
Type = PinType.Place,
Location = new Location(p.Latitude, p.Longitude)
}).ToList();
//Add the current location to ensure the area is centered around it
if (IncludeCurrentLocation)
{
var currentLocation = await GeoLocationHelper.GetLocation();
workPinList.Add(new Pin() { Location = currentLocation });
}
//Build the mapspan.
DisplayArea = GetMapSpan(workPinList);
//Remove the pin as it will be shown natively via the map
if (IncludeCurrentLocation)
{
workPinList.RemoveAt(workPinList.Count - 1);
}
PinsToMap = new ObservableCollection<Pin>(workPinList);
if (PinsToMap.Count == 1)
{
SubTitle = PinsToMap[0].Label;
}
else
{
SubTitle= string.Concat(AppResources.NumberResultsCaption, " ", PinsToMap.Count.ToString());
}
if (WorkingMap != null)
{
//Now create and position the map
MoveMap();
}
}
catch (Exception ex)
{
App.ProcessException(ex);
}
}
I have also ensured the list is being populated before the OnAppearing code runs and I moved the adding of the MarkerClick event to before the list is bound to the map.
Any ideas regarding why I it seems the MarkerClick event code is not firing?
I believe I figured out the problem. All the samples I could find for MarkerClicked were for pins being manually added to the map. I am using databinding to a list of items. It appears in the databinding case you need to specify the MarkerClicked event handler in the ItemTemplate of the Pin datatemplate as shown below from my XAML):
<maps:Map x:Name="map" ItemsSource="{Binding PinsToMap}" Margin="0,4,0,4" >
<maps:Map.ItemTemplate>
<DataTemplate x:DataType="{x:Type maps:Pin}">
<maps:Pin Location="{Binding Location}"
Address="{Binding Address}"
Label="{Binding Label}"
-->This is the line MarkerClicked="Pin_MarkerClicked"/>
</DataTemplate>
</maps:Map.ItemTemplate>
</maps:Map>
Unfortunately, I could not quickly figure out how to bind this to my VM's command so I simply had the code behind event call the command in my VM. Regardless, now my MarkerClicked event fires when a pin is clicked.