我目前正在努力使用Xamarin对上述API进行极其基本的实现。大多数清单都是从这些交叉引用的教程中复制过来的:
http://docs.xamarin.com/guides/android/platform_features/maps_and_location/part_2_-_maps_api/ https://developers.google.com/maps/documentation/android/start#obtain_a_google_maps_api_key
我的MapFragment的Map属性为null,我无法弄清楚原因。我已成功生成API密钥,并指定了我的包权限,包括将SHA-1指纹/包名称添加到API控制台。这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="@string/package_name">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="18" />
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" />
<!-- Google Maps for Android v2 requires OpenGL ES v2 -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<!-- We need to be able to download map tiles and access Google Play Services-->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Allow the application to access Google web-based services. -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Google Maps for Android v2 will cache map tiles on external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Google Maps for Android v2 needs this permission so that it may check the connection state as it must download data -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- These are optional, but recommended. They will allow Maps to use the My Location provider. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application android:label="@string/app_name">
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="@string/api_key_android" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
MAPS_RECIEVE是本教程中的权限,但我发现它现在已经过时,因此省略了它: Android Map V2 - Why MAPS_RECEIVE permission
READ_GSERVICES被检测为未知权限。
这是我的代码:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
//Create the user interface in code
LinearLayout layout = new LinearLayout (this);
layout.Orientation = Orientation.Vertical;
TextView txtKey = new TextView(this);
txtKey.Text = "Your key is " + GetString(Resource.String.api_key_android);
TextView txtNullCheck = new TextView (this);
txtNullCheck.Text = "Map not null";
//Create MapFragment and add it to this UI
MapFragment myMapFrag = MapFragment.NewInstance();
if (myMapFrag.Map != null) {
FragmentTransaction fragTx = FragmentManager.BeginTransaction();
fragTx.Add(layout.Id, myMapFrag);
fragTx.Commit();
}
else
{
txtNullCheck.Text = "Null GoogleMap";
}
layout.AddView(txtKey);
layout.AddView(txtNullCheck);
SetContentView(layout);
}
有趣的是,如果我省略空检查并且只是添加MapFragment,那么应用程序会在几秒钟后给我发消息“不幸的是,此应用程序已停止工作”。否则我只会获得一个null myMapFrag.Map,而MapFragment将无法正常运行。
我目前正在Galaxy Note 3上测试它。
我已经被困了很长一段时间了,而且我发现的文档并不是很有帮助。所以任何帮助都将非常感激!
谢谢,
〜F