Smartgatto
Lurker
Hello everyone ,
I'm developing an app to display the coordinates of my phone and I would use the google api for localization ( FusedLocationProvider ) but without using the GPS, only coordinates by wifi or GSM (network?) , so I thought to use PRIORITY_LOW_POWER or PRIORITY_NO_POWER, but if I deactivate GPS gives me error ( NULL ), whereas if the active GPS gives me the coordinates . How can I do?
thanks !
I'm developing an app to display the coordinates of my phone and I would use the google api for localization ( FusedLocationProvider ) but without using the GPS, only coordinates by wifi or GSM (network?) , so I thought to use PRIORITY_LOW_POWER or PRIORITY_NO_POWER, but if I deactivate GPS gives me error ( NULL ), whereas if the active GPS gives me the coordinates . How can I do?
thanks !
Code:
public class MainActivity extends FragmentActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener{
TextView tvLocation;
GoogleApiClient mGoogleApiClient;
Location locationUp;
LocationRequest mLocationRequest=new LocationRequest();
boolean requestingLocationUpdate=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
tvLocation = (TextView) findViewById(R.id.longitude_label);//last location
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();//be connected with GoogleApiClient
}
@Override
public void onConnected(Bundle bundle) {
updateUI();
if(requestingLocationUpdate){
startLocationUpdate();//trying to get the update location
}
}
private void startLocationUpdate() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
private void updateUI() {
tvLocation.setText(String.valueOf(getLocation().getLatitude())+String.valueOf(getLocation().getLongitude()));
}
private Location getLocation() {
Location lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
return lastKnownLocation;
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdate();//when activity is on pause, need to stop the location update since we
//do not need that any more
}
private void stopLocationUpdate() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);//stopping the update location
}
//no need to use googleplayservices
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();//disconnect the GoogleApiClient from the google play services
}
@Override
protected void onResume() {
super.onResume();
if(mGoogleApiClient.isConnected()){
startLocationUpdate();// since we need to get the update location continuously like gps
//when activity is in foreground(resume), then we have to request to get update location
//here alos
}
}
//to get the location change
@Override
public void onLocationChanged(Location location) {
locationUp=location;//this location is the update location,
// came from the requestLocationUpdate() method’s interface
updateView();//put your value in the text or whatever you want to do
}
private void updateView() {
// tvLocationUpdate.setText(“Update Location:”+String.valueOf(locationUp.getLatitude())+String.valueOf(locationUp.getLongitude()));
}
}