【问题标题】:Xamarin Google Maps BlankXamarin 谷歌地图空白
【发布时间】:2017-04-26 09:03:08
【问题描述】:

我一直在关注:

https://developer.xamarin.com/guides/android/platform_features/maps_and_location/maps/obtaining_a_google_maps_api_key/

https://developer.xamarin.com/guides/android/platform_features/maps_and_location/maps/obtaining_a_google_maps_api_key/

为了在安卓应用中使用谷歌地图。而且我根本无法在我的应用程序中显示地图。它总是显示一个空白页。我也没有看到任何错误。

我有:

  1. 为我的 android 清单添加了必要的权限
  2. 在线启用api页面中的API。
  3. 添加了基于使用本地调试密钥库的凭据
  4. 在我的主要活动页面上添加了地图作为地图片段。

网上的一切都指向调试密钥不正确,但我一定已经用这个尝试了所有我能做的事情,但它仍然不起作用。它会以某种方式选择不同的吗?

另外,我不确定它的重要性,但是当我尝试访问它时,mapfragment 在恢复时总是为空。

密钥库命令:

keytool -list -v -keystore "C:\Users\<My User folder>\AppData\Local\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

清单中的权限(隐藏 AppName 和密钥):

  <!-- 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" />
    <!-- Permission to receive remote notifications from Google Play Services -->
    <!-- Notice here that we have the package name of our application as a prefix on the permissions. -->
    <permission android:name="APPNAME.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="APPNAME.permission.MAPS_RECEIVE" />
    <!-- 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="<app name>">
        <!-- Put your Google Maps V2 API Key here. -->
        <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="MYKEY" />
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
    </application>

主要活动内容:

<?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" />

【问题讨论】:

    标签: c# android google-maps xamarin


    【解决方案1】:

    为了在安卓应用中使用谷歌地图。而且我根本无法在我的应用程序中显示地图。它总是显示一个空白页。我也没有看到任何错误。

    这可能是由以下原因引起的:

    1. 在这里查看参考How can I create a keystore? 应用程序签名后,地图将无法使用。

    2. 当您使用不同的计算机构建应用程序时,请从新计算机获取 SHA-1。

    3. 不要使用由 Java 生成的密钥库。请使用 Visual Studio 生成的密钥库。它在C:\Users\username\AppData\Local\Xamarin\Mono for Android/

    4. 使用包含 Google API 的模拟器。

    5. 不要忘记在 Google Console 中启用 Google API。

    这个Xamarin Android Google Map demo 应该对你有帮助。我已经用我自己的密钥库和 API_KEY 尝试了这个演示,它可以工作。

    【讨论】:

    • 谢谢,它为我修复了它的演示
    【解决方案2】:

    如果您启用 Google Play 应用签名,则您需要使用上传 apk 后提供的密钥库详细信息。

    发布管理 -> 应用签名(选项卡)

    应用签名证书是您所需要的。复制 SHA-1 证书指纹。

    【讨论】:

    • 我必须这样做,并将我的谷歌帐户与结算帐户相关联
    【解决方案3】:

    我已通过在 Google 控制台中将我的项目配置为启用来解决此问题 适用于 Android 的地方 SDK 和 Android 版地图 SDK

    等待几分钟,然后 BOOM !

    有时您可能需要在设备上重新安装该应用。

    【讨论】:

      【解决方案4】:

      当前的答案对我来说不太适用。我误解了文档 -https://docs.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/obtaining-a-google-maps-api-key?tabs=windows - 的意思:

      此 API KEY 必须是来自 google 开发者控制台的 API KEY,而不是您用来生成密钥库的 SHA-1。虽然这对你们大多数人来说可能很明显,但希望这对将来的其他用户有所帮助。

      【讨论】: