0

I've dev my first application and i make a simple map activity, but latitude and longitude are always 0. (when i start the activity the cursor should be set in the device location). I set permission in manifest and get google-services.json file, but i still have this error:

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.

I think this is why I cant get correct longitude and latitude.

Activity code:

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));
}
}

Manifest:

<?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>

Gradle:

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'
}

Reading the log, i saw "Permission:problems" so the app seems to not has the right permission.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
ghiltanas
  • 127
  • 1
  • 2
  • 13

2 Answers2

0

You need to place the configuration file (google-services.json) generated by developer.google.com, as mentioned in the 2nd step of the official docs here

Please take a look at this post: GoogleService failed to initialize

It will help you

Community
  • 1
  • 1
intellignt_idiot
  • 1,962
  • 2
  • 16
  • 23
  • Okay, can you send your source code as zip here, if possible ? I can look at it if there is something else wrong with the code – intellignt_idiot Jul 04 '16 at 16:39
  • The problem you are telling has a solution in the above post but if there is any other issue in your code, its hard to tell – intellignt_idiot Jul 04 '16 at 16:46
  • Try [https://github.com/yayaa/LocationManager] this lib , it might be easier for you to implement : – intellignt_idiot Jul 04 '16 at 16:54
  • Thanks for your help! I'll try it asap and i tell you if it works...hwe all' these permissions and keys are a nightmare – ghiltanas Jul 04 '16 at 16:59
  • i have no more that error line, i added selective dependencies in gradle, for ex: compile 'com.google.android.gms:play-services-maps:8.4.0' , but i still have problems with permission..i reply with my new class – ghiltanas Jul 05 '16 at 16:44
0

I think i solved my problem: - i updated android studio and now i use compile 'com.google.android.gms:play-services-maps:9.2.0' compile 'com.google.android.gms:play-services:9.2.0'

instead of 8.4.0.

Now i'll create a new post, cause i'm going crazy to use maps

ghiltanas
  • 127
  • 1
  • 2
  • 13