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
Yes, you cannot add properties in Pin class directly, please change another name for
Pin
(such asCustomPin
) then inherit Xamarin.Forms.GoogleMaps.Pin ,after that, you can add properties like following code.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
@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.
@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.
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
@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; }
}
customMap.Pins.Add(pin1.Pin);
Although you will also need a way to associate that Pin back to your ExtendedPin.
@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 ?
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.
@JoeManke The issue is that I have to use xamarin.forms.googlemaps because of the extended apis such as "selected pin".
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.
@JoeManke Do you know how I could do that in xamarin.forms.maps?
@JoeManke But that will only work after the label of a pin is clicked (after 2 clicks), or?
Yes, but I have a pull request working on that as well. https://github.com/xamarin/Xamarin.Forms/pull/6079
@JoeManke How long do you think it will take before they update it?
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.
@JoeManke Is it possible to achieve it with custom renderers?
Absolutely, just copy the code from the PR.
@JoeManke thank you, I will take a look at it and try to implement it.
hey guys, how i make custom message box for google map pin in xamarin?
like when i tapped on pin the my location and all other details show me ---->> how to make this custom ??
@SimpleBoy https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map-pin
@JoeManke this link is use the Xamarin.Forms.Map Library but i need to use Xamarin.Forms.GoogleMap Library
@JoeManke how can do that using this Xamarin.Forms.GoogleMap Library ??