Я новичок в xamarin, и мне нужно поместить карту Google в приложение.Xamarin Android Google Maps Fragment
Я скачал пример из Xamarin из:
https://github.com/xamarin/monodroid-samples/tree/master/MapsAndLocationDemo_v2
и после нескольких изменений, он работал, и этот проект отображает карту.
здесь код проекта, который работает:
/MainActivity.cs/
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Gms.Common;
using Android.OS;
using Android.Util;
using Android.Views;
using Android.Widget;
using AndroidUri = Android.Net.Uri;
namespace SimpleMapDemo
{
[Activity(Label = "@string/app_name", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
public static readonly int InstallGooglePlayServicesId = 1000;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.BasicDemo);
}
}
}
/BasicDemo.axml/
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
/AndroidManifest. xml/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionName="4.0" package="com.xamarin.docs.android.mapsandlocationdemo2" android:versionCode="6">
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="14" />
<!-- 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" />
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="com.xamarin.docs.android.mapsandlocationdemo2.permission.MAPS_RECEIVE" />
<permission android:name="com.xamarin.docs.android.mapsandlocationdemo2.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<application android:label="@string/app_name" android:icon="@drawable/icon">
<!-- Put your Google Maps V2 API Key here. This key will not work for you.-->
<!-- See https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key -->
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="apikeytest" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
Проблема заключается в том, чем тогда, когда я пытаюсь передать этот код в существующий проект, где я хочу, чтобы добавить карты, при выполнении его бросает исключение:
Android.Views.InflateException: Binary XML файл строка # 1: Ошибка фрагмент класса накачивания
код, который не удается:
/SplashScreen.cs/
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cirrious.MvvmCross.Droid.Views;
using Android.Content.PM;
//using Android.Gms.Common;
using AndroidUri = Android.Net.Uri;
using Android.Util;
using Android.Gms.Common;
using Android.Support.V4.App;
using Android.Gms.Maps;
namespace ProjectWithMap
{
[Activity(
Label = "ProjectWithMap",
MainLauncher = true,
Icon = "@drawable/icon",
//Theme ="@style/Theme.Splash",
ScreenOrientation = ScreenOrientation.Portrait,
NoHistory = true)]
public class SplashScreen : Activity
{
public static readonly int InstallGooglePlayServicesId = 1000;
private MapFragment map;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.provaMapa);
}
}
}
/provaMapa.axml/
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="ProjectWithMap.SplashScreen"
class="com.google.android.gms.maps.MapFragment" />
/AndroidManifest.xml/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="projectWithMap.projectWithMap" android:versionCode="6" android:versionName="4.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" />
<!-- 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" />
<!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
<uses-permission android:name="projectWithMap.projectWithMap.permission.MAPS_RECEIVE" />
<permission android:name="projectWithMap.projectWithMap.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<application android:label="projectWithMap" android:icon="@drawable/Icon" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
<!-- Google Maps V2 API Key -->
<!-- Para más documentación mirar https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key -->
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="key" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
</application>
</manifest>
И этот код бросает мне исключение:
Android.Views.InflateException : Двоичная строка XML-файла # 1: ошибка раздувания фрагмента класса
Я не понимаю, почему в одном проекте работает и в другом dosen't, я попытался найти эту ошибку и нашел несколько решений, пробовал все, что мне нравилось, без хороших результатов ...
change com. google.android.gms.maps.MapFragment для com.google.android.gms.maps.SupportMapFragment
изменения общественного класса SplashScreen: активность для общественного класса SplashScreen: FragmentActivity
, но ничего не получалось, всегда ту же ошибку.
Что мне не хватает? Почему он работает в одном проекте, а не в другом? ...
EDIT
кажется, что ошибка в манифесте, в точке, когда я исполняю, на выходе приложения:
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="yourKey" />
как это получает манифест службы Google. ..
пока я добавил свой ключ!
<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="key" />
Какой манифест является приложением, манифест приложения, манифест службы google?
EDIT:
В конце концов была ошибка в Xamarin или визуальной студии, удаление манифеста и создание снова решить эту проблему.
Вы определили/включили класс 'SampleMapActivity' в свой проект? Демо-проект использует это [здесь] (https://github.com/xamarin/monodroid-samples/blob/master/MapsAndLocationDemo_v2/SimpleMapDemo/SampleMapActivity.cs) для реализации макета, на котором будет включен фрагмент. – Koh
I не определял/не включал SampleMapActivity, но поскольку он показывает код, который работает, я не использую его, в MainActivity я делаю: SetContentView (Resource.Layout.BasicDemo); напрямую, поэтому я перехожу от основной деятельности к представлению basicdemo, и это работает в тестовом проекте, но не в реальном проекте. –
В конце концов, была ошибка xamarin или визуальной студии, удаление манифеста и создание его снова решило проблему. –