无法通过Android应用访问谷歌服务器

时间:2014-07-24 17:59:03

标签: java android google-maps android-fragments maps

我正在开发一个可以访问地图API的Android应用程序,但我坚持错误:

  

Google Maps Android API:无法加载地图。联系Google时出错   服务器。这可能是一个身份验证问题(但可能是由于   网络错误)。

已经在Google API控制台中使用密钥并启动了永久性。

这些是我的文件:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hflopes.mapas" >

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"></uses-permission>

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        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.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyBTObQnY1mxYwXw1g8txH5ApBu8ovxv730" />
        <activity android:name=".MostraAlunosProximos"></activity>
        <activity
            android:name=".mapas"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

mapas_layout:

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mapa"/>


</LinearLayout>

加载地图的类:

package hflopes.mapas;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by HFLopes on 24/07/2014.
 */
public class MostraAlunosProximos extends FragmentActivity{

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

        FragmentManager manager = getSupportFragmentManager();

        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mapa, new MapaFragment());
        transaction.commit();

    }
}

任何想法?

感谢您的帮助

3 个答案:

答案 0 :(得分:0)

我这样做了,并开始在我的设备上工作。试试吧。

<强>清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.example.mapsexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:maxSdkVersion="20"
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <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" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />  

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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="br.com.example.mapsexample.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="your_api_key_here" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

加载地图的类

package br.com.example.mapsexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MainActivity extends Activity {

    GoogleMap mGoogleMap;

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

        try {
            this.initializeMap();
        } catch ( final Exception e ) {
            // TODO: handle exception
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        this.initializeMap();
    }

    private void initializeMap() {
        if ( this.mGoogleMap == null ) {
            this.mGoogleMap = ( ( MapFragment ) this.getFragmentManager().findFragmentById( R.id.mapFragment ) ).getMap();

            if ( this.mGoogleMap == null ) {
                Toast.makeText( this.getApplicationContext(), "Is not possible load the map", Toast.LENGTH_SHORT ).show();
            }

        }

    }
}

布局地图

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

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

</RelativeLayout>

答案 1 :(得分:0)

我做了同样的事。

在你的MostraAlunosProximos课程中创建一个oncreate,

int status = GooglePlayServicesUtil
                .isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if (status != ConnectionResult.SUCCESS) { // Google Play Services
                                                    // are
                                                    // not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status,
                    this, requestCode);
            dialog.show();

        } else { // Google Play Services are available

            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.mapa);

            // Getting GoogleMap object from the fragment
            if (fm != null) {
                googleMap = fm.getMap();
            }

// Enabling MyLocation Layer of Google Map
            googleMap.setMyLocationEnabled(true);
}

并在xml文件的android:name="com.google.android.gms.maps.SupportMapFragment"内添加此行fragment。 而且我也看到谷歌播放服务有两个metadeta删除它。

希望这有帮助

答案 2 :(得分:0)

解决它!在build.gradle

上缺少这个
 signingConfigs {
        debug {
            storeFile file("debug.jks")
        }
    }

感谢您的帮助