简体   繁体   中英

Not able to fetch current latitude and longitude in android

I am new in android and i am trying to get my current location latitude and longitude and for this i am using ' com.google.android.gms:play-services:8.4.0 ' and i am getting this error
java.lang.IllegalArgumentException: GoogleApiClient parameter is required . and i referred to previous stckoverflow answers but it didnot helped me.Please help me, thanks in advance

and here is the code of Mapsactivity.java

   public class MapsActivity extends FragmentActivity{

   private GoogleMap mMap;
AppLocationService appLocationService;
GoogleApiClient mGoogleApiClient;
private Location mLastLocation;

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

  private void setUpMapIfNeeded() {
    if (mMap == null) {

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

        if (mMap != null) {

            displayLocation();
        }
    }
}
private void displayLocation() {

    mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

    if (mLastLocation != null) {
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();

        Log.d("result", latitude + ", " + longitude);

    } else {

      Log.d("result2","(Couldn't get the location. Make sure location is
   enabled on the device)");
     }
 }
}

And here is the code of my manifest file

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.malli.myapplication" >

<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_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_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.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/google_maps_key" />

    <activity
        android:name=".MapsActivity"
        android:label="@string/title_activity_maps" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

you have not initialized your mGoogleApiClient variable. please intialize it first.

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API).build();

 mLastLocation = LocationServices.FusedLocationApi
            .getLastLocation(mGoogleApiClient);

Note : this is just a sample to initialize GoogleApiClient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM