Xamarin.forms: [Bug] Xamari.Forms.Maps Custom Pins are null if they are data bound

Created on 3 Jun 2019  Â·  10Comments  Â·  Source: xamarin/Xamarin.Forms

Description

I want to use custom pins with Xamarin.Forms.Maps. I followed the official document and sample. It works fine if the pin data is provided in code behind as done in official sample.
But when pin is data bound with ViewModel data it does not work. In this scenario the NullReferenceException is thrown.

Basic Information

Reproduction Link

MapBindings.zip

Please add Google map Api Key in the Android Manifest before building the project.

docs maps help wanted inactive proposal-accepted enhancement âž• up-for-grabs

Most helpful comment

@samhouts the @jcmanke's answer solved the problem. The documentation is outdated. The documentation does not map with the changes in the code.

All 10 comments

You never initialize your CustomMap.CustomPins collection, but you try to loop through it in your renderers.

Maybe the sample is just out of date, but you don't need a separate list for your CustomPins, so you can probably just delete your CustomMap class and use your custom renderer for the default Map class. Iterate through the Map.Pins list instead of customPins.

@jcmanke I deleted the CustomMap from the project. The Android works fine. But the app never hits any breakpoint on iOS renderer.

@jcmanke
So, for iOS I use CustomMap. But iterate through the Map.Pins. It worked. For Android I am using default Renderer for Map. Android too works fine. The official sample needs update. Thank you very much.

Still this issue needs to be resolved. Iterating through default Map.Pins work if there is no additional requirement. But if a new property is needed in a pin e.g URL than the use of custom pins become inevitable.

Wrong. Having your ItemTemplate return a CustomPin works just fine, you will just have to cast it when you pull it out. The need for a separate CustomPins list ended 2 years ago, when I made a pull request that unsealed the Pin class.

@jcmanke Great, can you please point to the resource where I can learn more about maps in xamarin.forms. It is so difficult to get correct and updated information. You have a good command over this subject.

There is lots of discrepancy betweeen iOS and Android. In Android I didn't need Custom Maps. I also didn't iterate through the pins. I simply set the Marker properties and it is done.
Whereas on iOS I still need CustomMap class and I need to iterate through all the pins.
Here is the sample.

@HobDev Can you please clarify this issue for me now? Is your original issue still valid, or are you satisfied with @jcmanke's answer? Is the problem now that we need better documentation on how to extend Pins? Thanks!

@samhouts the @jcmanke's answer solved the problem. The documentation is outdated. The documentation does not map with the changes in the code.

Excellent! Thanks for the update!

Was this page helpful?
0 / 5 - 0 ratings