当前位置: 首页 > 知识库问答 >
问题:

Xamarin Android谷歌地图片段

怀飞掣
2023-03-14

我是xamarin的新手,我需要在应用程序中放置谷歌地图。

我已经从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" />

/Android智能.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.膨胀异常:二进制 XML 文件行 #1:inflating类片段时出错

失败的代码:

/启动屏幕.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" />

/Android智能.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.膨胀异常:二进制 XML 文件行 #1:inflating类片段时出错

我不明白为什么在一个项目中工作,而在另一个项目中不工作,我试图搜索这个错误,并找到了多种解决方案,尝试了所有我喜欢的没有好结果...

更改网站

更改公共类SplashScreen:公共类splash screen:fragmentation Activity的活动

但是没有任何效果,总是同样的错误。

我错过了什么?为什么它在一个项目中工作,而不是在另一个项目中工作?...

编辑

似乎错误在清单中,在我执行时,在应用程序输出处:

<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="yourKey" />

就像获取谷歌服务清单一样...

虽然我已经添加了我的钥匙!

<meta-data android:name="com.google.android.maps.V2.API_KEY" android:value="key" />

应用程序获取、应用程序清单和谷歌服务清单是什么?

编辑:

最后是xamarin或Visual Studio的错误,删除清单并再次创建它解决了问题。

共有2个答案

詹正浩
2023-03-14

添加<代码>

呼延宪
2023-03-14

添加额外的工具

“Google Play服务”

在Android Sdk 管理器中...并注意只有一个标签“应用程序”(拼写良好)

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
    <meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/mapkey"/>
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    <uses-library android:name="org.apache.http.legacy" android:required="false"/>
</application>
 类似资料:
  • 我有问题加载一个谷歌地图在我的片段。一切都从一个循环器视图适配器开始,该适配器启动活动“ViatGedetAllViewActivity”: 这是我的DetallViewActivity: =2 03-24 02:55:53.881 639-7362/com.example.usuari.MyApplication3 w/ResourcesManager:GetTopleVelResources:

  • 我在我的应用程序中实现了一个谷歌地图片段,用户可以通过点击右上角的“指南针”按钮看到他们当前的位置。

  • 情况是我使用Fragments在main活动中显示2个视图,这意味着我有两个选项卡,而在第二个选项卡中,我使用以下代码显示谷歌地图 这是第二个选项卡,我想自定义地图,为此我在上面的函数中使用了这段代码。 imap=SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap(); imap是在创建

  • 我对Android开发真的是一个新手,有几天我会尝试在我的应用程序中添加一个地图。我尝试了一些事情,但没有成功。如果有人能帮我,我很感激。 我做过的事: 将google-play-services_lib添加到我的包资源管理器中。 包含了android-support-v4.jar和google-play-services.jar作为我的项目的依赖项。 下面是main.xml文件 这里是清单文件

  • 所以我看到了一个特殊的例外,我敢肯定的是谷歌地图的绘图代码。 我有一个片段,在那里我以编程方式添加了一个支持地图片段,然后我在其中操纵谷歌地图实例。 这是stacktrace: 我无法可靠地复制它(尽管这种情况经常发生),我已经查看了ReadWriteDirectByteBuffer和ShortToByteBufferAdapter,但没有任何东西突然出现在我面前。 有什么想法吗?

  • 我认为问题是我需要添加构建提示。我可以找到在哪里添加他们,但我不确定他们应该是什么格式。我也不知道要补充什么。我想我发现了一个帖子,说我需要添加Android.xapplication=Android:value=“Your Key”/>Android.xPermissions= 但我不确定构建提示条目表单中的格式是什么