I am trying to implement the users location in my android project. I have tried alot on many of the codes found in the internet but nothing worked. Neither the Location Manager nor the Location client also tried the new one GoogleApiClient too. But i don't know where is my mistake. Do we require any of the Api's key to get users location? I have kept my codes below: Location_Finder2.java
package com.notification.messaging;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
public class Location_Finder2 extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener{
LocationRequest locationRequest;
LocationClient locationClient;
Location location;
Double longitude = 0.0d;
Double latitude = 0.0d;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationClient = new LocationClient(this, this, this);
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(1000);
}
@Override
public void onLocationChanged(Location arg0) {
locationClient.removeLocationUpdates(this);
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.i("New Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
Log.i("Location_2","dhsjchdjcjdjcjkdjcjdn");
location = locationClient.getLastLocation();
if (location == null){
locationClient.requestLocationUpdates(locationRequest, this);
}
else {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.i("Latitude && Longitude:", "Longitude:" + longitude + ", Latitude:" + latitude);
}
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
}
I am not getting any location update waiting for a long time but i don't find what my mistake is.