Xamarin.Forms.GoogleMaps cannot derive from sealed type pin. Trying to add custom properties to pin

Jane_NatalieJane_Natalie Member ✭✭✭

I am currently trying to add custom properties to my pin class, and I have to use xamarin.forms.google.maps because of its "Selected pin" event.

public class Pin : Xamarin.Forms.GoogleMaps.Pin // cannot derive from a sealed type "pin".
{
public string CustomProperty { get; set; }
public string CustomProperty2 { get; set; }
public string CustomProperty3 { get; set; }
public string CustomProperty4 { get; set; }

}

So how can I add custom properties? Can I access the nuget package code and unseal it?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    Yes, you cannot add properties in Pin class directly, please change another name for Pin(such as CustomPin) then inherit Xamarin.Forms.GoogleMaps.Pin ,after that, you can add properties like following code.

    using Xamarin.Forms.Maps;
    
    namespace CustomRenderer
    {
      public class CustomPin : Pin
      {
          public string Url { get; set; }
      }
    }
    

    There is article about custom pin.
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map/customized-pin#creating-the-custom-map

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @LeonLu
    This is my actual code, I just made that pin name as an example, sorry for the inconvenience.
    public class customPin : Xamarin.Forms.GoogleMaps.Pin
    {
    public string Adr { get; set; }
    public bool Taken { get; set; }
    public int Number { get; set; }
    }

    I am aware of how to derive from the pin class, but in xamarin.forms.googlemaps it is not possible to do it as if I was using xamarin.forms.maps, because the pin class is sealed :/ I need to use xamarin.forms.googlemaps because of a selected pin event. The issue is that I don't know how to add custom properties to the pin class, as it is sealed.

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    @Jane_Natalie
    Oh, I see, you can not directly inherit from Pin, but you can create a new class ExtendedPin with a field of type Pin.

    public class ExtendedPin
    {
       public Pin Pin { get; set; }
    
        // Add your custom fields here
    }
    

    You can refer to this link
    https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/70

    If you want to custom a Pin, you can see above link as well, check the [dinonovak's reply] , he create a custom info window renderer to achieve that (https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/70#issuecomment-323525152 "dinonovak's reply").

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @LeonLu said:
    @Jane_Natalie
    Oh, I see, you can not directly inherit from Pin, but you can create a new class ExtendedPin with a field of type Pin.

    public class ExtendedPin
    {
       public Pin Pin { get; set; }
    
        // Add your custom fields here
    }
    

    You can refer to this link
    https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/70

    If you want to custom a Pin, you can see above link as well, check the [dinonovak's reply] , he create a custom info window renderer to achieve that (https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/70#issuecomment-323525152 "dinonovak's reply").

    @LeonLu Hi, so I changed the class like you said, but now I get a different error saying:
    "Argument 1: cannot convert from 'App2.Models.ExtendedPin' to 'Xamarin.Forms.GoogleMaps.Pin'".

    This error appears as I try to add a pin to the map:

    var pin1 = new ExtendedPin
    {
    Pin = new Pin { Position = position, Label = "example"},
    Taken = true,
    Adr = "ex",
    };
    customMap.Pins.Add(pin1);
    Is there maybe another way to add the pin? Or is it possible to unseal the pin class?
    Thanks for the help,
    Jane

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @LeonLu The source https://github.com/javiholcman/Wapps.Forms.Map/blob/master/Wapps.Forms.Map/WPin.cs is using xamarin.forms.maps and not xamarin.forms.googlemaps in the example where the pin property is set like this:
    public class ExtendedPin
    {
    public Pin Pin { get; set; }

    // Add your custom fields here
    

    }

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    @Jane_Natalie said:
    @LeonLu Hi, so I changed the class like you said, but now I get a different error saying:
    "Argument 1: cannot convert from 'App2.Models.ExtendedPin' to 'Xamarin.Forms.GoogleMaps.Pin'".

    This error appears as I try to add a pin to the map:

    var pin1 = new ExtendedPin
    {
    Pin = new Pin { Position = position, Label = "example"},
    Taken = true,
    Adr = "ex",
    };
    customMap.Pins.Add(pin1);
    Is there maybe another way to add the pin? Or is it possible to unseal the pin class?
    Thanks for the help,
    Jane

    customMap.Pins.Add(pin1.Pin);

    Although you will also need a way to associate that Pin back to your ExtendedPin.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke Yeah. Or is it possible to unseal the pin class? Or perhaps add the pins in a different manner, for instance map.pins = listOfExtendedPin ?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Unsealing the Pin class is something that would have to be done via a pull request to their GitHub repo, then waiting until they release an update with that change. Someone has already taken the first step of filing an issue requesting it: https://github.com/amay077/Xamarin.Forms.GoogleMaps/issues/637

    Or you could just use Xamarin.Forms.Maps which does not have a sealed Pin class because I had the same issue and took care of it 2 years ago.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke The issue is that I have to use xamarin.forms.googlemaps because of the extended apis such as "selected pin".

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    What does their SelectedPin do besides keep a reference to the last pin you tapped? You could probably replicate it in Xamarin.Forms.Maps fairly easily.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke Do you know how I could do that in xamarin.forms.maps?

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    pin.Clicked += PinClicked;
    
    private void PinClicked(object sender, EventArgs e)
    {
        SelectedPin = (Pin)sender;
    }
    
  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke But that will only work after the label of a pin is clicked (after 2 clicks), or?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Yes, but I have a pull request working on that as well. https://github.com/xamarin/Xamarin.Forms/pull/6079

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke How long do you think it will take before they update it?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    If we bother them enough, it might make it into the next release (4.2.0) but realistically it will probably be the release after that.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke Is it possible to achieve it with custom renderers?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Absolutely, just copy the code from the PR.

  • Jane_NatalieJane_Natalie Member ✭✭✭

    @JoeManke thank you, I will take a look at it and try to implement it.

Sign In or Register to comment.