Computer
Computer

Reputation: 2227

Label and pins not showing using Xamarin.Forms.Map

VS 2019 with all updates. I install Xamarin.Forms.Map using nuget.

Using Xamarin Android to display a map with a pin point.

Front end XAML is

<ContentPage.Content>
    <maps:Map x:Name="Map"/>
</ContentPage.Content>

In code-behind i have

    public TestPage()
    {
        InitializeComponent();

        var map = new Map(MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(10)));

        var pin = new Pin()
        {
            Position = new Position(37, -122),
            Label = "Some Pin!"
        };
        map.Pins.Add(pin);

        var cp = new ContentPage
        {
            Content = map,
        };
    }

Which i have followed from MSDN https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.maps.map?view=xamarin-forms.

I then click pixel_2_pie_api_28 (F5) and once the page loads it shows a map but not to the location i set (i randomly changed the position values) or the pin point or even label? What am i missing?

Upvotes: 1

Views: 2030

Answers (1)

Jason
Jason

Reputation: 89102

you have quite a few problems in your code

   // you already have a map declared in your XAML, you do not need to do it again
   var map = new Map(MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(10)));

    var pin = new Pin()
    {
        Position = new Position(37, -122),
        Label = "Some Pin!"
    };

    // add this to your existing XAML map, which you named "Map"
    Map.Pins.Add(pin);

    // you already have a ContentPage, you don't need to declare a new one
    var cp = new ContentPage
    {
        Content = map,
    };

all you need to do

    var pin = new Pin()
    {
        Position = new Position(37, -122),
        Label = "Some Pin!"
    };

    Map.Pins.Add(pin);

to move the map to a specific position

var span = MapSpan.FromCenterAndRadius(new Position(37, -122), Distance.FromMiles(10));
Map.MoveToRegion(span);

Upvotes: 2

Related Questions