c# - 使用 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),一旦页面加载,它会显示地图,但不会显示到我设置的位置(我随机更改了位置值)或针点甚至标签?我错过了什么?
解决方案
你的代码有很多问题
// 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);
推荐阅读
- r - 在 Ggplot 中添加一个不与先前参数重叠的图层
- javascript - 通过检查响应来处理 Promise 中的错误
- javascript - 从 JS 到 PHP 的 POST 请求:错误的 JSON
- python-3.x - 从 MongoDB 集合中的一组文档中创建一个列表
- scientific-computing - 使用 nextflow 创建奇点容器
- javascript - 如何在html中使用滚动条来动态分配按钮?
- java - JDBC 查找旧表名
- docker - Docker - dockerfile 中的多个卷
- sql-server - 我可以在 Azure Sql 数据库和 Sql Server 2012(本地服务器)之间创建复制吗?
- c# - 我怎样才能做出正确的 if/else if/else 语句?