I am stuck with location with GPS/Network Providers. I know it is a basic question for all androiders but i am having this problem since long time.
Actually I am taking current location and inserting in database by following method on click of Option Menu .
/**** * GPS Process First of all call listener of Location then checking for GPS_PROVIDER if not * available then check for NETWORK_PROVIDER and if its also not available then pass * 0.00,0.00 to longitude and latitude */ /** PROCESS for Get Longitude and Latitude **/ locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); // Define a listener that responds to location updates locationListener = new LocationListener() { public void onLocationChanged(Location location) { // Called when a new location is found by the network location provider. longitude = String.valueOf(location.getLongitude()); latitude = String.valueOf(location.getLatitude()); Log.d(TAG, "changed Loc : " + longitude + ":" + latitude); } public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { } public void onProviderDisabled(String provider) { } }; // getting GPS status isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // check if GPS enabled if (isGPSEnabled) { int i; for (i = 0; i < 3; i++) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); Location location = locationManager .getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { longitude = String.valueOf(location.getLongitude()); latitude = String.valueOf(location.getLatitude()); break; } else { location = locationManager .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { longitude = String.valueOf(location.getLongitude()); latitude = String.valueOf(location.getLatitude()); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); break; } else { longitude = "0.00"; latitude = "0.00"; } } } /** * Record Inserting Process in Database... **/ } else { AlertDialogManager.showAlertforSettings(AccountMenuActivity.this, "GPS Settings", "GPS is not Enabled. Do you want to Enable?", false); }
I am getting 0.00 latitude and longitude many times and sometimes i got the location of other countries (China, South Africa etc...)
https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi
That i have implemented by following some links but getting errors like:
/** * Booleans for GPS and NETWORK is Enabled or not... */ boolean gpsEnabled = false; boolean networkEnabled = false; //exceptions will be thrown if provider is not permitted. try { gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch (Exception ex) { Log.e("TAG", "GPS Error : " + ex.getLocalizedMessage()); } try { networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch (Exception ex) { Log.e("TAG", "NetWork Error : " + ex.getLocalizedMessage()); } Log.e("TAG", "GPS Network status : " + gpsEnabled + " : " + networkEnabled); //don t start listeners if no provider is enabled if (!gpsEnabled && !networkEnabled) { AlertDialogManager.showAlertforSettings(VisitAcitivity.this, "GPS Settings", "GPS is not Enabled. Do you want to Enable?", false); } else { Log.i(TAG, "isPlayServiceAvailable" + isPlayServiceAvailable); if(isPlayServiceAvailable) { createLocationRequest(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); } else { final MyLocation.LocationResult locationResult = new MyLocation.LocationResult() { @Override public void gotLocation(final Location location) { // Got the location! runOnUiThread(new Runnable() { @Override public void run() { longitude = String.valueOf(location.getLongitude()); latitude = String.valueOf(location.getLatitude()); saveVisit(); } }); } }; MyLocation myLocation = new MyLocation(); myLocation.getLocation(VisitAcitivity.this, locationResult); } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); if(isPlayServiceAvailable && mGoogleApiClient != null) stopLocationUpdates(); else if (locationManager != null) { locationManager.removeUpdates(locationListener); Log.d("msg", "Location Listener ended..."); } } @Override public void onStop() { super.onStop(); Log.d(TAG, "onStop fired .............."); if(mGoogleApiClient != null) mGoogleApiClient.disconnect(); } @Override public void onConnected(Bundle bundle) { Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected()); startLocationUpdates(); } protected void startLocationUpdates() { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); Log.d(TAG, "Location update started ..............: "); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } @Override public void onLocationChanged(Location location) { Log.d(TAG, "Firing onLocationChanged.............................................."); mCurrentLocation = location; Log.d(TAG, "UI update initiated ............."); if (null != mCurrentLocation) { latitude = String.valueOf(mCurrentLocation.getLatitude()); longitude = String.valueOf(mCurrentLocation.getLongitude()); } else { Log.d(TAG, "location is null ..............."); } } protected void stopLocationUpdates() { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); Log.d(TAG, "Location update stopped ......................."); }
In that i am managing two way to get location:
- Google Play Service is available or not then get location using FusedLocation API.
- Otherwise Get location using My own way (because so many device).
I had taken following permissions in My AndroidManifest.xml:
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
then also getting NULL locations :( IS THERE ANY SOLUTION for THIS PROBLEM?
Thanking you in Advance.