I'm having trouble retrieving a value when a PIN is selected in Android. I tried using dependency injection, but it didn't work. Is there another approach I could try? Also, could you share the code? I've already read all the documentation for dependency injection, but I still can't retrieve the value.
Shared Project interface
public delegate void PinRetrievedEventHandler(IMapPin pin);
public class CustomPlatformService
{
public static event PinRetrievedEventHandler PinRetrieved;
// Method to raise the PinRetrieved event
public static void RaisePinRetrieved(IMapPin pin)
{
PinRetrieved?.Invoke(pin);
}
}
mMain Page ViewModel
private readonly CustomPlatformService _platformService;
constructor inject intermediate class
_platformService = DependencyService.Get<CustomPlatformService>();
CustomPlatformService.PinRetrieved += (pin) => HandlePinRetrieved(pin, null);
Declare on MauiProgram.CS
builder.Services.AddSingleton<CustomPlatformService>();
builder.ConfigureMauiHandlers(handlers =>
{
handlers.AddHandler<Microsoft.Maui.Controls.Maps.Map, CustomMapHandler>();
});
This is the android class that I would like to return the pin id to the shared project on HandlePinRetrieved.
public class CustomMarkerClickListener(MapSettings mapHandler) : Java.Lang.Object, GoogleMap.IOnMarkerClickListener
{
int selected_location_id;
public bool OnMarkerClick(Marker marker)
{
if (marker.Tag is Java.Lang.Integer tag)
{
int pinId = tag.IntValue();
// Access the custom number (PinId) associated with the marker
Console.WriteLine($"Marker clicked with PinId: {pinId}");
selected_location_id = pinId;
var pin = mapHandler.Markers.FirstOrDefault(x => x.marker.Id == marker.Id);
pin.pin?.SendMarkerClick();
marker.ShowInfoWindow();
//marker.NotifyAll(); here I need to send that info as an alert form to the main shared project
}
return true; //
}
I also tried this method, the only issue I have is that I have to pass the interface to the constructor and I cant do that because I will have to do several other changes.it has to be a easier way to do it.