If you take a look on the logcat, the error stated that you have imported wrong LocationListener. Check on your imported library at the top part of the class, it supposed to be com.google.android.gms.location.LocationListener not android.location.LocationListener.
It's quite simple actually, you are trying to pass context as LocationListener. Instead change locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context ); to locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Your error is here:
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );
^^^^^^^^^^^^^^^^^^^^^^^^^^^
context holds instance of type com.onedevz.noct.MainActivity which does not implement LocationListener. Replace (LocationListener) context with locationListener which is your location change handler implementation.
You should implement LocationListener and don't try to cast your activity to LocationListener.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
Implement the LocationListener methods
And replace this
(LocationListener) this
To this
this
you need to implement LocationListener in you MapsActivity class. then you can do as flows : replace this line
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
with this :
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
and this line :
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this);
with this :
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
Make sure your DataActivity implements com.google.android.gms.location.LocationListener and not android.location.LocationListener.
You may have imported android.location.LocationListener by mistake.
Instead of public class DataActivity extends FragmentActivity implements LocationListener, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{
do
public class DataActivity extends FragmentActivity implements **com.google.android.gms.location.LocationListener**, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{
after that you have to implement all its methods
Try This class for location
import com.arco.util.location.GPSTracker;
import com.arco.util.location.LocationUtils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
public class MYActivity extends Activity implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
// Global variable to hold the current location
Location mCurrentLocation;
// Request to connect to Location Services
private LocationRequest mLocationRequest;
// Stores the current instantiation of the location client in this object
private LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Create a new global location parameters object
mLocationRequest = LocationRequest.create();
// Set the update interval
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
mLocationClient = new LocationClient(this, this, this);
} // End OnCreate()
@Override
public void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
public void onStop() {
mLocationClient.disconnect();
super.onStop();
}
/**
* Verify that Google Play services is available before making a request.
*/
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
return true;
} else {
return false;
}
}
@Override
public void onConnectionFailed(ConnectionResult mConnectionResult) {
if (mConnectionResult.hasResolution()) {
try {
// Start an Activity that tries to resolve the error
mConnectionResult.startResolutionForResult(this, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
}
@Override
public void onConnected(Bundle connectionHint) {
try {
if (servicesConnected()) {
mLocationClient.requestLocationUpdates(mLocationRequest, this);
// Get the current location
Location currentLocation = mLocationClient.getLastLocation();
if (currentLocation != null) {
mCurrentLocation = currentLocation;
}
mCurrentLocation.getLatitude();
mCurrentLocation.getLongitude()
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected() {
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
}
}
@Moradiya Akash, @Karan Mavadhiya, @pratt : Thankyou so much for helping. I was trying to display the contents of location as charequence in the line
loc.setText((CharSequence) location);
Once I removed it, the app works fine. Thanks Vny Kumar for helping me debug the code myself, it would help me in a long run. :)
Try adding the below :
compile 'com.google.android.gms:play-services:9.8.0'
in your app level gradle dependencies and
classpath 'com.google.gms:google-services:3.1.0'
in project level gradle dependencies
note the versions may change based on your SDK config or your preference
Things has changed since 2018, and Gradle 3.0.0. Now you must use:
implementation instead of compile, in your ./app/build.gradle, in the dependencies{} section. Like this:
dependencies {
...
implementation 'com.google.android.gms:play-services-location:16.0.0'
...
}
Search for location in the releases web page.