我对 Visual Studio/Xamarin 还是很陌生,但我有一个简单的 OpenStreetMap 示例在不久前为 Android 运行,它确实显示了地图,但似乎不再显示地图,尽管我没有更改此示例的代码据我记得有一段时间。我不确定是否某些 Visual Studio/Xamarin 或 MacOS 更新可能导致它不再工作。
我在 macOS Mojava 10.14.6 和以下软件包上使用 Visual Studio for Mac community 8.4.4 (build 91):
- Mapsui 1.4.8
- OsmSharp 6.2.0
- Xamarin.Android.Support.Core.Utils 28.0.0.1
- Xamarin.Android.Support.CustomTabs 28.0.0.1
- Xamarin.Android.Support.Design 28.0.0.1
- Xamarin.Essentials 1.2.0
我的MainActivity.cs
文件包含以下代码:
using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
using Mapsui;
using Mapsui.Geometries;
using Mapsui.Projection;
using Mapsui.Styles;
using Mapsui.Utilities;
using Mapsui.UI.Android;
using Mapsui.Widgets.ScaleBar;
using Mapsui.Widgets.Zoom;
using Xamarin.Essentials;
namespace mvp_android
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.Main);
var mapControl = FindViewById<MapControl>(Resource.Id.mapcontrol);
var map = new Mapsui.Map();
map.Layers.Add(OpenStreetMap.CreateTileLayer());
map.Widgets.Add(
new ZoomInOutWidget(map) {
HorizontalAlignment = Mapsui.Widgets.HorizontalAlignment.Left,
VerticalAlignment = Mapsui.Widgets.VerticalAlignment.Top,
Orientation = Mapsui.Widgets.Zoom.Orientation.Horizontal,
}
);
var centerOfLondonOntario = new Point(-81.2497, 42.9837);
map.NavigateTo(SphericalMercator.FromLonLat(centerOfLondonOntario.X, centerOfLondonOntario.Y));
map.NavigateTo(map.Resolutions[9]);
mapControl.Map = map;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
MenuInflater.Inflate(Resource.Menu.menu_main, menu);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
int id = item.ItemId;
if (id == Resource.Id.action_settings)
{
return true;
}
return base.OnOptionsItemSelected(item);
}
private void FabOnClick(object sender, EventArgs eventArgs)
{
View view = (View)sender;
Snackbar.Make(view, "Replace with your own action", Snackbar.LengthLong)
.SetAction("Action", (Android.Views.View.IOnClickListener)null).Show();
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
我的Main.xml
包含以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Mapsui.UI.Android.MapControl
android:id="@+id/mapcontrol"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
最初,此示例显示 OpenStreetMap 的地图以安大略省伦敦的位置为中心,带有放大/缩小按钮,但现在它只在地图应有的位置显示白色,仍显示放大/缩小按钮并显示 (c) OpenStreetMap 水印。
我在这里想念什么?