You are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Be sure that the LocationListener you're implementing is from the google api, that is import this:
import com.google.android.gms.location.LocationListener;
and not this:
import android.location.LocationListener;
and it should work just fine.
It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html
Answer from HigiPha on Stack OverflowYou are implementing LocationListener in your activity MainActivity. The call for concurrent location updates will therefor be like this:
mLocationClient.requestLocationUpdates(mLocationRequest, this);
Be sure that the LocationListener you're implementing is from the google api, that is import this:
import com.google.android.gms.location.LocationListener;
and not this:
import android.location.LocationListener;
and it should work just fine.
It's also important that the LocationClient really is connected before you do this. I suggest you don't call it in the onCreate or onStart methods, but in onResume. It is all explained quite well in the tutorial for Google Location Api: https://developer.android.com/training/location/index.html
I use this one:
LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
For example, using a 1s interval:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
the time is in milliseconds, the distance is in meters.
This automatically calls:
public void onLocationChanged(Location location) {
//Code here, location.getAccuracy(), location.getLongitude() etc...
}
I also had these included in the script but didnt actually use them:
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
In short:
public class GPSClass implements LocationListener {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
}
}
LocationManager.NETWORK_PROVIDER need network, but it is real, not precise. if you want to use LocationManager.GPS_PROVIDER, the situation must be outdoor instead of indoor, because GPS location need satellite, if you are in any building, the satellite cannot find you! pleasle go outdoor and check with GPS_PROVIDER again!
*best approach is getting location from both of GPS and NETWORK and check any of then that was not null and more accurate useing it.
For the getLastKnownLocation method, it is said in the documentation :
If the provider is currently disabled, null is returned.
Which means your GPS is disabled.
For the requestLocationUpdates, you are requesting a location every 20 seconds, try to decrease this number so you know at least if your program is working.
You can try this :
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Also I really advise you to use the new API to request location updates : https://developer.android.com/training/location/receive-location-updates.html
It is very fast and accurate, but you need to create a google developer account (for free) and create an api key. All needed information should be in the link.
Samsung phones have this problem. There is hack to this. You need to kick the locationmanager on the butt to get the latest location from the maps cache.
God.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
currentLocation = God.locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Call getLastKnownLocation after kicking the requestLocationUpdates with 0's.
It happens with emulators, try rebooting the emulator.
Or, close both eclipse and emulator and start both again
It will update the existing request.
You could register it multiple times. see my code below.
package com.test.locationmanager;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;
public class LocationManagerStatus extends Activity {
private LocationManager locationManager;
private TextView textView;
private final LocationListener gpsLocationListener =new LocationListener(){
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "GPS available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "GPS out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt + "GPS temporarily unavailable\n");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Enabled\n");
}
@Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "GPS Provider Disabled\n");
}
@Override
public void onLocationChanged(Location location) {
locationManager.removeUpdates(networkLocationListener);
textView.setText(textView.getText().toString()
+ "New GPS location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};
private final LocationListener networkLocationListener =
new LocationListener(){
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
final String tvTxt = textView.getText().toString();
switch (status) {
case LocationProvider.AVAILABLE:
textView.setText(tvTxt + "Network location available again\n");
break;
case LocationProvider.OUT_OF_SERVICE:
textView.setText(tvTxt + "Network location out of service\n");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
textView.setText(tvTxt
+ "Network location temporarily unavailable\n");
break;
}
}
@Override
public void onProviderEnabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Enabled\n");
}
@Override
public void onProviderDisabled(String provider) {
textView.setText(textView.getText().toString()
+ "Network Provider Disabled\n");
}
@Override
public void onLocationChanged(Location location) {
textView.setText(textView.getText().toString()
+ "New network location: "
+ String.format("%9.6f", location.getLatitude()) + ", "
+ String.format("%9.6f", location.getLongitude()) + "\n");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.textview);
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 0,
networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000, 0, gpsLocationListener);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(networkLocationListener);
locationManager.removeUpdates(gpsLocationListener);
}
}
According to the docs :
The elapsed time between location updates will never be less than
minTime
So minTime takes precedence. Further on it says :
The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed
So it should be if(time >= minTime AND dist >= minDistance), meaning won't check distance if too early. BUT don't take those parameters too seriously before JellyBeans.
As per my understanding it should be OR. Read this blog for more info.
Keep in mind that GPS has an accuracy of 10 to 50 meters itself.
user this Handler to get Location
private Handler customHandler = new Handler();
private Runnable updateTimerThread = new Runnable() {
@Override
public void run()
{
try
{
Location myLocation = mGoogleMap.getMyLocation();
secndLocationListener.onLocationChanged(myLocation);
}
catch (Exception e)
{
Log.getStackTraceString(e);
}
}
};
call this
customHandler.postDelayed(updateTimerThread , 10 * 60 * 1000);
The best way to do this is not through requestLocationUpdates, but rather through using a timer to start updates periodically. That should spare you from wearing out your battery needlessly. So I end up doing something like this:
- I have some checks to see if I like the GPS value.
- If I'm satisfied with the GPS, then I remove updates, and start a timer to start getting updates again.
- I always get updates upon resuming the application.
Code to restart the GPS updates might look like this:
Timer mTimer;
private void sendFinalGps() {
mLocationManager.removeUpdates(this);
mTimer = new Timer();
mTimer.schedule(new findGpsTime(), getTimeBetweenUpdates());
}