2016-07-04 48 views
0

我開發了我的第一個應用程序,我做了一個簡單的地圖活動,但經度和緯度始終是0.(當我開始活動時,光標應設置在設備位置)。 我在清單設置權限,並獲得谷歌-services.json文件,但我仍然有此錯誤:無法獲取當前位置問題(android,權限)

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.

我想這就是爲什麼我不能得到正確的經度和緯度。

活動代碼:

public class ********Activity extends FragmentActivity implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private GoogleMap mMap; 
double latitude, longitude; 
GoogleApiClient mGoogleApiClient; 
Location mLastLocation; 

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

    buildGoogleApiClient(); 

    if (mGoogleApiClient != null) { 
     mGoogleApiClient.connect(); 
    } else 
     Toast.makeText(this, "Not connected...", Toast.LENGTH_SHORT).show(); 

    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
} 

protected void onStart() { 
    mGoogleApiClient.connect(); 
    super.onStart(); 
} 

protected void onStop() { 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 


public void onConnectionFailed(ConnectionResult arg0) { 
    Toast.makeText(this, "Failed to connect...", Toast.LENGTH_SHORT).show(); 

} 


public void onConnectionSuspended(int arg0) { 
    Toast.makeText(this, "Connection suspended...", Toast.LENGTH_SHORT).show(); 

} 


public void onConnected(Bundle arg0) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     Log.i("Permission","problems"); 
     return; 
    } 

    if (mLastLocation != null) { 
     latitude = mLastLocation.getLatitude(); 
     longitude = mLastLocation.getLongitude(); 
     Log.i("latitude",""+latitude); 
     Log.i("longitude",""+longitude); 
    } 

} 


protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 


/** 
* 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. 
*/ 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    LatLng pos = new LatLng(latitude,longitude); 
    mMap.addMarker(new MarkerOptions().position(pos).title("User Marker")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(pos)); 
} 
} 

清單:

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

<!-- To auto-complete the email text field in the login form with the user's emails --> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
<uses-permission android:name="android.permission.READ_PROFILE" /> 
<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_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. 
--> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/soccer" 
    android:label="@string/app_name" 
    android:launchMode="singleTop" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <meta-data 
     android:name="com.facebook.sdk.ApplicationId" 
     android:value="@string/facebook_app_id" /> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 
    <meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <activity 
     android:name="com.facebook.FacebookActivity" 
     android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 
    <activity 
     android:name=".MainActivity" 
     android:theme="@style/AppTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".LoginActivity" 
     android:label="@string/title_activity_login" 
     android:parentActivityName=".MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.marcocreation.******.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".HomeUserActivity" 
     android:label="@string/title_activity_home_user" 
     android:theme="@style/AppTheme" /> 
    <activity 
     android:name=".RegisterActivity" 
     android:label="@string/title_activity_register" 
     android:parentActivityName=".MainActivity" 
     android:theme="@style/AppTheme"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.marcocreation.*******.MainActivity" /> 
    </activity> 
    <activity 
     android:name=".ProfileActivity" 
     android:label="@string/title_activity_profile" 
     android:theme="@style/AppTheme" /> 
    <activity 
     android:name=".profileActivity2" 
     android:label="@string/title_activity_profile2" 
     android:theme="@style/AppTheme" /> 
    <!-- 
     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/. 
    --> 


    <activity 
     android:name=".locateMatchActivity" 
     android:label="@string/title_activity_locate_match"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.marcocreation.********.profileActivity2" /> 
    </activity> 
</application> 

</manifest> 

搖籃:

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 

    compile 'com.android.support:appcompat-v7:23.4.0' 
    compile 'com.android.support:design:23.4.0' 
    compile 'com.facebook.android:facebook-android-sdk:4.+' 
    compile 'com.google.android.gms:play-services:8.4.0' 
} 

閱讀日誌,我看到 「權限:問題」 這樣的應用程序似乎不具有正確的許可。

+0

你可以在這裏發佈日誌嗎? –

+0

如果您的設備是android-M,那麼您需要授予運行時權限。 – Drv

+0

[GoogleService無法初始化]的可能重複(http://stackoverflow.com/questions/34365369/googleservice-failed-to-initialize) –

回答

0

您需要將配置文件通過developer.google.com產生,如官方文檔的第2步中提到here

請看看這個帖子(谷歌services.json):GoogleService failed to initialize

它會幫助你

+0

已經創建json文件並放在應用程序目錄中 – ghiltanas

+0

好的,如果可能的話,你可以在這裏以zip的形式發送源代碼嗎?我可以看看它是否有代碼 –

+0

對不起,我不能.. – ghiltanas

0

我想我解決我的問題: - 我更新的Android工作室,現在我使用 編譯「com.google.android.gms:發揮服務,地圖:9.2.0」 compile'c​​om.google.android.gms:pla y-services:9.2.0'

而不是8.4.0。

現在我將創建一個新帖子,因爲我要瘋狂使用地圖