1

I got a map with a list of pins and I'd like to highlight one of them programatically, simulating a click on it.

public MapViewModel(INavigationService navigationService, IApiAutostradeManagerFactory apiAutostradeFactory) : base(navigationService, apiAutostradeFactory)
{
   Map = new Map
  {
    MapType = MapType.Street,
  };
  Map.PinClicked += Map_PinClicked;
}

private void Map_PinClicked(object sender, PinClickedEventArgs e)
    {
        Console.WriteLine("I clicked it!");
    }
}

This event is raised correctly everytime i click on a pin, but i'd like to raise it directly from code. I'm using this this Map Library: https://github.com/amay077/Xamarin.Forms.GoogleMaps

Giuseppe Pennisi
  • 396
  • 3
  • 22

2 Answers2

0

According to the link you gave, there's a method called SendPinClicked which do what you want but is internal. It takes a pin as a parameter.

You can call it even if it's internal with reflection (as you can find here : How do I use reflection to invoke a private method)

So you could probably create an extension method like this (Didn't tested, can't right now) :

static class MapExtensions
{
    public static void callPinClicked(this Map m, Pin p)
    {
        var mi = m.GetType().GetMethod(SendPinClicked, System.Reflection.BindingFlags.NonPublic  | System.Reflection.BindingFlags.Instance );
        if (mi != null) {
            mi.Invoke (m, new object[] { p } );
        }
    }
}

Then you could do

Map.callPinClicked(_pin);
Grégory L
  • 612
  • 6
  • 17
  • Thanks for the answer, i called the callPinClicked but nothing happens. I explored more source code, and i find that there is a class called `PinLogic` that contains `OnMakerClick`method that use `SendPinClicked` method, but I'm a bit confused about how them work togheter. Any hint? – Giuseppe Pennisi Aug 21 '19 at 12:09
  • Could you try to debug and see if mi != null on the code I gave ? – Grégory L Aug 21 '19 at 12:12
  • I noticed that event is triggered, because when i call the method `callPinClicked`, the method `Map_PinClicked` is fired and print in console the string. The problem is that pin is not selected on map. – Giuseppe Pennisi Aug 21 '19 at 12:32
  • Well you ask about how to invoke the event that what we did :P Great you found your solution :) – Grégory L Aug 21 '19 at 13:49
0

I got the solution: Map contains the property SelectedPin. It is enough find the desired pin and set SelectedPin. To simulate camera moving after a click, It's necessary use the following method: Map.MoveToRegion(MapSpan.FromCenterAndRadius(selectedPin.Position, Distance.FromKilometers(15)));

Giuseppe Pennisi
  • 396
  • 3
  • 22