【问题标题】:Failed to load map. Error contacting Google servers on fragment加载地图失败。在片段上联系 Google 服务器时出错
【发布时间】:2013-07-24 19:59:46
【问题描述】:
  1. 我用谷歌搜索了两天,仍然是这条消息。我使用调试密钥进行调试 在我的关系 7 上。
  2. 我不知道哪里错了。我有右键,打开正确的api访问
  3. 但我平板电脑上的谷歌地图仍然空白。

地图片段:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,

捆绑 savedInstanceState) {
查看根视图 = inflater.inflate(R.layout.map_fragment,container, false); 容器.removeAllViews();地图= ((SupportMapFragment)getActivity() .getSupportFragmentManager() .findFragmentById(R.id.map)) .getMap(); map.setMyLocationEnabled(true); map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

   mMapFragment = SupportMapFragment.newInstance();
   FragmentTransaction fragmentTransaction = 

getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.map,mMapFragment);
片段交易.commit();返回根视图; }

清单:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

<!-- Google Map -->   <permission
      android:name="com.jertt.xxx.permission.MAPS_RECEIVE"
      android:protectionLevel="signature"/>
<uses-permission android:name="com.jertt.yummymap.permission.MAPS_RECEIVE"/>

<!-- end -->

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<!-- end -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.jertt.yummymap.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <!-- Google Map API Key -->

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyB54eZnUp8Sw*****" />

    <!-- end -->
</application>

map_fragment.xml

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

【问题讨论】:

  • 保持原帖不变,将编辑部分放在编辑部分下,以免误导其他访问该帖子的人。
  • 对不起,但我改变了我的 MainActicity,所以问题不同了。
  • 新问题应作为新问题发布。它与您之前的问题完全不同。答案还会误导其他人关于发布答案的原始问题。
  • 当你在 xml 中有这个 class="com.google.android.gms.maps.MapFragment" 时,你还在 oncreateView 中使用 SupportMapFragmentgetSupportFragmentManager()
  • 对不起,我倒回过去的版本,现在问新问题。

标签: android google-maps


【解决方案1】:

不确定这是否是您的问题,但我花了很多时间试图解决这个问题。检查权限,重新生成 api 密钥。终于在另一个对我有用的答案中看到了一条小评论。

重命名您的 debug.keystore 文件,然后进行清理,然后构建。这将生成一个新的 debug.keystore。和一个新的 SHA1 指纹。将新指纹插入 api 控制台,然后重试。

您可以检查的另一件事是查看您尝试从中访问 api 的项目的 api 控制台的“报告”部分。如果您尝试运行您的项目并且该 api 没有流量,则可能是 SHA1 指纹或您提供的包名称错误。就我而言,它是指纹。

【讨论】:

  • 我不知道它是否解决了别人的问题,但解决了我的问题。谢谢老兄。
【解决方案2】:

你必须改成这样:

GoogleMap  mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
                            .getMap();

并且还包括清单文件的库:

<uses-library
        android:name="com.google.android.maps"
        android:required="true" />

【讨论】:

    【解决方案3】:

    您的最小 sdk 为 14。无需使用 SupportMapFragment。使用MapFragment

    https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment.

    <fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="fill_parent"
          android:name="com.google.android.gms.maps.MapFragment"/> 
    

    还有

      GoogleMap  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                                    .getMap();
    

    也缺少权限

     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
     <!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    

    确保您已遵循所有步骤@

    https://developers.google.com/maps/documentation/android/

    编辑:来自评论

    引用自文档

    仅当您以 API 12 及更高版本为目标时,才使用 MapFragment 类。 否则,请使用 SupportMapFragment。

    【讨论】:

    • 但是如果我想在 android 2.3.3、4.2.2 上同时运行这两个版本~我应该使用 supportMapFragment 对吗?
    • @user2585728 为此,您将不得不更改您的 min sdk,现在也是 14。如果您的 min sdk 低于 11,请使用支持地图片段
    • 我将其更改为 MapFragment。但是fragmentTransaction.add显示错误:FragmentTransaction类型中的方法add(int, Fragment)不适用于参数(int, MapFragment)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多