Google Maps V2无法在Android中呈现

时间:2014-10-31 11:10:18

标签: android google-maps

当我在Galaxy S3中运行时,出现了Google的名称和+/-缩放,但地图永远不会出现,请有人帮帮我!

我已经完成了三次所有步骤来生成API密钥。

我不知道我是否必须在Activity中执行某些操作,但我的活动扩展了FragmentActivity,但我总是尝试获取此地图,始终返回null ...

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

我的清单:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<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="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<permission
    android:name="com.example.portalrugby.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.portalrugby.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"   />

<uses-feature
    android:name="android.hardware.location"
    android:required="true" />
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="false"
    android:icon="@drawable/icon340x340"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="xxxxxxxxxxxx" />

我的activity_main.xml

   <RelativeLayout
        android:id="@+id/relativeMap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" >

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

    </RelativeLayout>

我的logcat:

10-31 09:22:47.520: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.b.d@4263b810 not retrying 
10-31 09:22:47.520: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.gmm6.d.h@426335b8 not retrying 
10-31 09:22:47.520: I/Google Maps Android API(23871): Failed to contact Google servers. Another attempt will be made when connectivity is established.
10-31 09:22:51.265: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.b.d@4365a3c0 not retrying
10-31 09:22:51.265: D/REQUEST(23871): Retrying: com.google.maps.api.android.lib6.b.j@4263e6f8
10-31 09:22:51.265: D/REQUEST(23871): Retrying: com.google.maps.api.android.lib6.c.au@426bbae8
10-31 09:22:51.475: D/REQUEST(23871): Connection opened  to:https://clients4.google.com/glm/mmap/api
10-31 09:22:51.475: D/REQUEST(23871): Open Connection
10-31 09:22:51.765: D/REQUEST(23871): DRD(9): 62|15|147
10-31 09:22:51.765: D/REQUEST(23871): Close

1 个答案:

答案 0 :(得分:0)

Java页面代码

package com.dilip.googlemapsv2;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    // Google Map
    private GoogleMap googleMap;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
    * function to load map. If map is not created it will create it for you
    * */
   private void initilizeMap() {
       if (googleMap == null) {
           //googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
           googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

           // check if map is created successfully or not
           if (googleMap == null) {
               Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
           }
       }
   }

   @Override
   public void onResume() {
       super.onResume();
       GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getApplicationContext());
       initilizeMap();
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

XML页码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <fragment
        android:id="@+id/map"

        android:name="com.google.android.gms.maps.MapFragment"      
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>



</RelativeLayout>

Menefist页码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dilip.googlemapsv2"
    android:versionCode="1"
    android:versionName="1.0" >
    <permission
        android:name="com.dilip.googlemapsv2.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="20" />
    <uses-permission android:name="com.dilip.googlemapsv2.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />



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



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

        <!-- Goolge API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />                /* Added Bi Dilip */ 
        <meta-data 
            android:name="com.google.android.gms.version" 
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>