【发布时间】:2021-07-19 23:23:42
【问题描述】:
我正在将 Huawei Kit 集成到我的应用中,但我很挣扎,因为它根本不调用 OnMapReady:
这是我的代码:
XML:
<com.huawei.hms.maps.MapView
android:id="@+id/map"
app:mapType="normal"
app:liteMode="true"
app:uiCompass="true"
app:uiZoomControls="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
C#:
public class HmsLazyInputStream : LazyInputStream
{
public HmsLazyInputStream(Context context)
: base(context)
{
}
public override Stream Get(Context context)
{
try
{
return context.Assets.Open("agconnect-services.json");
}
catch (Exception e)
{
Log.Error("Hms", $"Failed to get input stream" + e.Message);
return null;
}
}
}
[ContentProvider(new string[] { "tk.supernovaic.themayanroute.XamarinCustomProvider" })]
public class XamarinCustomProvider: ContentProvider
{
public XamarinCustomProvider()
{
}
public override int Delete(Android.Net.Uri uri, string selection, string[] selectionArgs)
{
throw new NotImplementedException();
}
public override string GetType(Android.Net.Uri uri)
{
throw new NotImplementedException();
}
public override Android.Net.Uri Insert(Android.Net.Uri uri, ContentValues values)
{
throw new NotImplementedException();
}
public override bool OnCreate()
{
AGConnectServicesConfig config = AGConnectServicesConfig.FromContext(Context);
config.OverlayWith(new HmsLazyInputStream(Context));
return false;
}
public override ICursor Query(Android.Net.Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder)
{
throw new NotImplementedException();
}
public override int Update(Android.Net.Uri uri, ContentValues values, string selection, string[] selectionArgs)
{
throw new NotImplementedException();
}
}
这是活动:
private MapView mapView;
private const string MAPVIEW_BUNDLE_KEY = "MY_KEY";
MapsInitializer.Initialize(Activity);
//I tried my ID and my API id and nothing.
MapsInitializer.SetApiKey("MY_API_FROM_THE_WEBSITE?");
mapView = (MapView)view.FindViewById(Resource.Id.map);
mapView.OnCreate(savedInstanceState);
mapView.GetMapAsync(this);
public override void OnPause()
{
base.OnPause();
mapView.OnPause();
}
public override void OnDestroy()
{
base.OnDestroy();
mapView.OnDestroy();
}
public override void OnSaveInstanceState(Bundle outState)
{
base.OnSaveInstanceState(outState);
Bundle mapViewBundle = outState.GetBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null)
{
mapViewBundle = new Bundle();
outState.PutBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mapView.OnSaveInstanceState(mapViewBundle);
}
public override void OnLowMemory()
{
base.OnLowMemory();
mapView.OnLowMemory();
}
public override void OnResume()
{
base.OnResume();
mapView.OnResume();
}
public override void OnDestroyView()
{
base.OnDestroyView();
}
public async void OnMapReady(HuaweiMap googleMap)
{
}
这是我的属性:
<meta-data android:name="com.huawei.hms.client.appid" android:value="appid=MY_ID" />
<uses-permission android:name="com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA" />
知道为什么它不起作用吗?
【问题讨论】:
-
嗨 Federico,根据上面提供的信息,很难重现该问题。您能否提供运行日志供我们检查此问题?
-
嗨@shirley,我能够修复它。问题是你必须在华为设备上运行它(不是说在任何地方)。现在,我有两个建议,您的团队是否有可能在 GitHub 中创建一些示例?它们更容易被编码人员阅读。此外,有没有什么方法可以让我们在未来使用云或任何替代映像来调试 Xamarin 应用程序?另外,有什么方法可以向地图团队提供反馈吗?因为图标太小了。谢谢。
-
嗨@Federico,很高兴听到问题已经解决。关于其他两个问题,我在下面发布我的答案,请参考。
标签: android xamarin.forms xamarin.android huawei-mobile-services huawei-map-kit