I am trying to add a pin to my Xamarin Forms Mapsui control.
Here is my Xaml
<mapsui:MapView x:Name="selectMapControl"
VerticalOptions="FillAndExpand"
HorizontalOptions="Fill"
BackgroundColor="Gray" />
Here is my code:
protected override void OnAppearing()
{
base.OnAppearing();
var map = new Map
{
CRS = "EPSG:3857",
Transformation = new MinimalTransformation()
};
var tileLayer = OpenStreetMap.CreateTileLayer();
map.Layers.Add(tileLayer);
map.Widgets.Add(new Mapsui.Widgets.ScaleBar.ScaleBarWidget(map) { TextAlignment = Mapsui.Widgets.Alignment.Center, HorizontalAlignment = Mapsui.Widgets.HorizontalAlignment.Left, VerticalAlignment = Mapsui.Widgets.VerticalAlignment.Bottom });
selectMapControl.Map = map;
double lat = 36.9628066;
double lng = -122.0194722;
var myPosition = new Position(lat, lng);
var myPin = new Pin(selectMapControl) //Exception here
{
Position = myPosition,
Type = PinType.Pin,
Label = "Zero point",
Address = "Zero point",
};
selectMapControl.Pins.Add(myPin);
}
I receive an "Object reference not set to an instance of an object.'" exception on the line var myPin = new Pin(selectMapControl)...
If my lat and lng are both set to 0 then it works
Here is the stack trace in the exception:
Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Mapsui.UI.Forms.Pin.set_Position (Mapsui.UI.Forms.Position value) [0x00000] in <5cc65edf513349cdba641e049bbf0143>:0
at eLunaApp.Views.RestaurantMapPage.OnAppearing () [0x0008d] in C:\x\elunaapp\eLunaApp\PageRestaurantMap\RestaurantMapPage.xaml.cs:82
at Xamarin.Forms.Page.SendAppearing () [0x00045] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:452
at Xamarin.Forms.ShellContent.SendPageAppearing (Xamarin.Forms.Page page) [0x0003b] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:127
at Xamarin.Forms.ShellContent.SendAppearing () [0x00019] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:108
at Xamarin.Forms.ShellContent.OnChildAdded (Xamarin.Forms.Element child) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:138
at Xamarin.Forms.ShellContent.set_ContentCache (Xamarin.Forms.Page value) [0x0003b] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:182
at Xamarin.Forms.ShellContent.Xamarin.Forms.IShellContentController.GetOrCreateContent () [0x0003c] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:68
at Xamarin.Forms.Platform.Android.ShellSectionRenderer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x000f2] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellSectionRenderer.cs:166
at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00021] in D:\a\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid90\generated\src\AndroidX.Fragment.App.Fragment.cs:1950
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.50(intptr,intptr,intptr,intptr,intptr)
at Mapsui.UI.Forms.Pin.OnPropertyChanged (System.String propertyName) [0x001df] in <5cc65edf513349cdba641e049bbf0143>:0
at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
at Mapsui.UI.Forms.Pin.set_Position (Mapsui.UI.Forms.Position value) [0x00000] in <5cc65edf513349cdba641e049bbf0143>:0
at eLunaApp.Views.RestaurantMapPage.OnAppearing () [0x0008d] in C:\x\elunaapp\eLunaApp\PageRestaurantMap\RestaurantMapPage.xaml.cs:82
at Xamarin.Forms.Page.SendAppearing () [0x00045] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:452
at Xamarin.Forms.ShellContent.SendPageAppearing (Xamarin.Forms.Page page) [0x0003b] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:127
at Xamarin.Forms.ShellContent.SendAppearing () [0x00019] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:108
at Xamarin.Forms.ShellContent.OnChildAdded (Xamarin.Forms.Element child) [0x00021] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:138
at Xamarin.Forms.ShellContent.set_ContentCache (Xamarin.Forms.Page value) [0x0003b] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:182
at Xamarin.Forms.ShellContent.Xamarin.Forms.IShellContentController.GetOrCreateContent () [0x0003c] in D:\a\1\s\Xamarin.Forms.Core\Shell\ShellContent.cs:68
at Xamarin.Forms.Platform.Android.ShellSectionRenderer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x000f2] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ShellSectionRenderer.cs:166
at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00021] in D:\a\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid90\generated\src\AndroidX.Fragment.App.Fragment.cs:1950
at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.50(intptr,intptr,intptr,intptr,intptr)