首页 > 解决方案 > 使用 Xamarin.Forms.Map 未显示标签和引脚

问题描述

VS 2019 的所有更新。我使用 nuget 安装 Xamarin.Forms.Map。

使用 Xamarin Android 显示带有定位点的地图。

前端 XAML 是

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

在代码隐藏中我有

    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,
        };
    }

我从 MSDN https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.maps.map?view=xamarin-forms遵循的。

然后我单击 pixel_2_pie_api_28 (F5),一旦页面加载,它会显示地图,但不会显示到我设置的位置(我随机更改了位置值)或针点甚至标签?我错过了什么?

标签: c#xamarin.formsmaps

解决方案


你的代码有很多问题

   // 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,
    };

所有你需要做的

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

    Map.Pins.Add(pin);

将地图移动到特定位置

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

推荐阅读