谷歌地图无法启动

时间:2016-01-19 13:02:40

标签: java android google-maps

01-19 20:30:59.245 18242-18242/kianyang.scbg E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
01-19 20:30:59.245 18242-18242/kianyang.scbg E/GMPM: Scheduler not set. Not logging error/warn.
01-19 20:30:59.295 18242-18290/kianyang.scbg E/GMPM: Uploading is not possible. App measurement disabled

我的代码编译成功。但谷歌地图无法启动。问题是什么?我尝试编译时会出现此错误。但编译很好,它无法启动谷歌地图。

MapsActivity.java

package kianyang.scbg;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity{

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
      setUpMapIfNeeded();
    }

@Override
    protected  void onResume(){
    super.onResume();
    setUpMapIfNeeded();

}

    private  void setUpMapIfNeeded(){

        if(mMap==null){

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

            if(mMap!=null){
                setUpMap();
            }
        }




    }
    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
   private void setUpMap(){
       mMap.addMarker(new MarkerOptions().position(new LatLng(2.215035, 102.245443)).title("Marker"));
   }
}

这是我的清单文件。里面有另一个意图;只是忽略它。请帮助查看Google地图代码。

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

    <permission
        android:name="kianyang.scbg.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <user-permission android:name="kianyang.scbg.permission.MAPS_RECEIVE" />

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



    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAljcGmDttsvHZjgpjFlsrsSfjTc6D9lkM" />

        <activity
            android:name=".Home"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Searchactivity"
            android:label="@string/title_activity_searchactivity">
            <intent-filter>
                <action android:name="kianyang.scbg.Searchactivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Vendoractivity"
            android:label="@string/title_activity_vendoractivity">
            <intent-filter>
                <action android:name="kianyang.scbg.Vendoractivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Cartactivity"
            android:label="@string/title_activity_cartactivity">
            <intent-filter>
                <action android:name="kianyang.scbg.Cartactivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Loginactivity"
            android:label="@string/title_activity_loginactivity">
            <intent-filter>
                <action android:name="kianyang.scbg.Loginactivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Signupactivity"
            android:label="@string/title_activity_signupactivity">
            <intent-filter>
                <action android:name="kianyang.scbg.Signupactivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".save_info_layout"
            android:label="@string/title_activity_save_info_layout" />
        <activity android:name=".DisplayProduct" />

        <intent-filter>
            <action android:name="kianyang.scbg.Locationactivity" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
    </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

您似乎错过了.XML文件中的Google Maps API密钥(这允许您访问Google服务器)。用于为android设置地图的开发博客是Here,步骤#4描述了如何获取密钥。

答案 1 :(得分:0)

对于此错误,您可能会遗漏google-services.json com.google.gms.google-services,或者您错过了正确的插件build.gradle。如果您有compile com.google.android.gms:play-services:8.4.0行,请检查com.google.android.gms:play-services:8.4.0文件(8.4.0是最新版本,但可以是8.3.0或更低版本)。如果没有,您可以通过SDK Manager下载它。

注意:如果您使用json=str(MyTableData) 进行进一步说明,则需要SD卡,请参阅link