我正在尝试将google map导入到我的xamarin.android应用程序中。我将密钥和编写的代码设置如下`
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Maps);
SetUpMap();
FindViewElements();
}
private void SetUpMap()
{
if (mMap == null)
{
var _mapFragment = FragmentManager.FindFragmentByTag("maptag") as MapFragment;//null
var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);//null
// var mapFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map);//Here are also null
// var _mapFragment = SupportFragmentManager.FindFragmentById(Resource.Id.map) as SupportMapFragment; /null
}
}
protected override async void FindViewElements()
{
_mapsModel = new MapsModel
{
MapsFragment = FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map),
MapTypeSpinner = FindViewById<Spinner>(Resource.Id.spinner),
Toolbar = FindViewById<Toolbar>(Resource.Id.mapToolbar),
};
SetSupportActionBar(_mapsModel.Toolbar);
SupportActionBar.Title = "Maps";
SupportActionBar.SetHomeButtonEnabled(true);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
_mapsModel.MapsFragment.GetMapAsync(this);
_mapsModel.MapTypeSpinner.ItemSelected += MapTypeSpinner_ItemSelected;
}
我的axml如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/header">
<include
android:id="@+id/mapToolbar"
layout="@layout/toolbar" />
</RelativeLayout>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/maptype_arrays" />
<fragment
android.id="@+id/map"
android:tag="maptag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
我尝试了所有可能的方法,例如将Fragment替换为suppportFragment,但没有任何帮助。谁能建议我实现这一目标。
预先感谢
答案 0 :(得分:0)
我尝试您的axml,并且得到的结果作为您,因此我建议您可以在FrameLayout中添加片段,以便可以通过ID获得该片段。像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/zoomInButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom In" />
<Button
android:id="@+id/zoomOutButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Zoom Out" />
<Button
android:id="@+id/animateButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Animate to Location" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</FrameLayout>
var mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
var mapFragment =(MapFragment)FragmentManager.FindFragmentById(Resource.Id.map);
您还可以查看有关在项目中导入Google地图的MSDN文档: https://docs.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/maps-api