-1

I am working on accessing the user current location and have gone through this tutorial Current location and this tutorial Current Location. Everything seems fine but latitude and longitude are not coming up in the TextView field.

Here is my code

@Keep
public class edit_information extends Fragment implements AdapterView.OnItemSelectedListener, GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener, ResultCallback<LocationSettingsResult> {
    protected final static String TAG = "EditInformation";
    private static final int MY_PERMISSIONS_ACCESS_LOCATION = 1;
    private RadioGroup radioGroup;
    private RadioGroup musicRadioGroup;
    private RadioGroup decorRadioGroup;
    protected static final int REQUEST_CHECK_SETTINGS = 0x1;
    //The desired interval for location updates
    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
    //The fastest rate for active location updates
    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
    //Keys for storing activity state in bundle
    protected static final String KEY_REQUESTING_LOCATION_UPDATES = "requesting-location-updates";
    protected static final String KEY_LOCATION = "location";
    protected static final String KEY_LAST_UPDATED_TIME_STRING = "last-updated-time-string";
    /*
    Provides entry point to Google play service
     */
    protected GoogleApiClient mGoogleApiClient;
    //Store parametrers for request to the FusedLocationProviderApi
    protected LocationRequest mLocationRequest;
    //location setting request
    protected LocationSettingsRequest mLocationSettingsRequest;
    protected Location mCurrentLocation;
    //UI
    protected TextView mLatitudeText;
    protected TextView mLongitudeText;
    protected Boolean mRequestingLocationUpdates;
    protected String mLastUpdateTime;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_information, container, false);
        Spinner spinner = (Spinner) view.findViewById(R.id.spinner1);
        Spinner spinner2 = (Spinner) view.findViewById(R.id.spinner2);

        //Creating an array adapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(), R.array.hall_type, simple_spinner_item);
        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(getContext(), R.array.hall_size, simple_spinner_item);

        //Layout to choose the dropdown list
        adapter.setDropDownViewResource(simple_spinner_dropdown_item);
        adapter1.setDropDownViewResource(simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner2.setAdapter(adapter1);
        spinner.setOnItemSelectedListener(this);
        spinner2.setOnItemSelectedListener(this);
        radioGroup = (RadioGroup) view.findViewById(R.id.food_facility);
        musicRadioGroup = (RadioGroup) view.findViewById(R.id.music_facility);
        decorRadioGroup = (RadioGroup) view.findViewById(R.id.decor_facility);
        mLatitudeText = (TextView) view.findViewById(R.id.myLatitude);
        mLongitudeText = (TextView) view.findViewById(R.id.myLongitude);
        mRequestingLocationUpdates = false;
        mLastUpdateTime = "";
        updateValuesFromBundle(savedInstanceState);
        //Start the process of building GoogleApiClient, LocationRequest and LocationSettingRequest
        buildGoogleApiClient();
        createLocationRequest();
        buildLocationSettingsRequest();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                switch (checkedId) {
                    case R.id.yes:
                        Toast.makeText(getContext(), "Yes", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.may:
                        Toast.makeText(getContext(), "May be", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.no:
                        Toast.makeText(getContext(), "No", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        musicRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.yesMusic:
                        Toast.makeText(getContext(), "Yes", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.mayMusic:
                        Toast.makeText(getContext(), "May be", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.noMusic:
                        Toast.makeText(getContext(), "No", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        decorRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i) {
                    case R.id.yesDecor:
                        Toast.makeText(getContext(), "Yes", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.mayDecor:
                        Toast.makeText(getContext(), "May be", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.noDecor:
                        Toast.makeText(getContext(), "No", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        return view;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(adapterView.getContext(), "" + adapterView.getItemAtPosition(i).toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    private void updateValuesFromBundle(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            // Update the value of mRequestingLocationUpdates from the Bundle, and make sure that
            // the Start Updates and Stop Updates buttons are correctly enabled or disabled.
            if (savedInstanceState.keySet().contains(KEY_REQUESTING_LOCATION_UPDATES)) {
                mRequestingLocationUpdates = savedInstanceState.getBoolean(
                        KEY_REQUESTING_LOCATION_UPDATES);
            }

            // Update the value of mCurrentLocation from the Bundle and update the UI to show the
            // correct latitude and longitude.
            if (savedInstanceState.keySet().contains(KEY_LOCATION)) {
                // Since KEY_LOCATION was found in the Bundle, we can be sure that mCurrentLocation
                // is not null.
                mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            }

            // Update the value of mLastUpdateTime from the Bundle and update the UI.
            if (savedInstanceState.keySet().contains(KEY_LAST_UPDATED_TIME_STRING)) {
                mLastUpdateTime = savedInstanceState.getString(KEY_LAST_UPDATED_TIME_STRING);
            }
            updateUI();
        }
    }

    protected synchronized void buildGoogleApiClient() {
        Log.i(TAG, "Building GoogleApiClient");
        mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    protected void buildLocationSettingsRequest() {
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        mLocationSettingsRequest = builder.build();
    }

    protected void checkLocationSettings() {
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(
                        mGoogleApiClient,
                        mLocationSettingsRequest
                );
        result.setResultCallback(this);
    }

    @Override
    public void onResult(LocationSettingsResult locationSettingsResult) {
        final Status status = locationSettingsResult.getStatus();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                Log.i(TAG, "All location settings are satisfied.");
                startLocationUpdates();
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" +
                        "upgrade location settings ");

                try {
                    // Show the dialog by calling startResolutionForResult(), and check the result
                    // in onActivityResult().
                    status.startResolutionForResult(getActivity(), REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    Log.i(TAG, "PendingIntent unable to execute request.");
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " +
                        "not created.");
                break;
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            // Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS:
                switch (resultCode) {
                    case Activity.RESULT_OK:
                        Log.i(TAG, "User agreed to make required location settings changes.");
                        startLocationUpdates();
                        break;
                    case Activity.RESULT_CANCELED:
                        Log.i(TAG, "User chose not to make required location settings changes.");
                        break;
                }
                break;
        }
    }

    public void startUpdatesButtonHandler(View view) {
        checkLocationSettings();
    }
    public void stopUpdatesButtonHandler(View view) {
        // It is a good practice to remove location requests when the activity is in a paused or
        // stopped state. Doing so helps battery performance and is especially
        // recommended in applications that request frequent location updates.
        stopLocationUpdates();
    }

    protected void startLocationUpdates() {
        if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient,
                mLocationRequest,
                this
        ).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                mRequestingLocationUpdates = true;
            }
        });
    }

    private void updateUI() {
        updateLocationUI();
    }

    private void updateLocationUI() {
        if (mCurrentLocation != null) {
            mLatitudeText.setText(String.valueOf(mCurrentLocation.getLatitude()));
            mLongitudeText.setText(String.valueOf(mCurrentLocation.getLongitude()));
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    @Override
    public void onResume() {
        super.onResume();
        // Within {@code onPause()}, we pause location updates, but leave the
        // connection to GoogleApiClient intact.  Here, we resume receiving
        // location updates if the user has requested them.
        if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
            startLocationUpdates();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        // Stop location updates to save battery, but don't disconnect the GoogleApiClient object.
        if (mGoogleApiClient.isConnected()) {
            stopLocationUpdates();
        }
    }

    protected void stopLocationUpdates() {
        // It is a good practice to remove location requests when the activity is in a paused or
        // stopped state. Doing so helps battery performance and is especially
        // recommended in applications that request frequent location updates.
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleApiClient,
                this
        ).setResultCallback(new ResultCallback<Status>() {
            @Override
            public void onResult(Status status) {
                mRequestingLocationUpdates = false;
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        mGoogleApiClient.disconnect();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.i(TAG, "Connected to GoogleApiClient");

        // If the initial location was never previously requested, we use
        // FusedLocationApi.getLastLocation() to get it. If it was previously requested, we store
        // its value in the Bundle and check for it in onCreate(). We
        // do not request it again unless the user specifically requests location updates by pressing
        // the Start Updates button.
        //
        // Because we cache the value of the initial location in the Bundle, it means that if the
        // user launches the activity,
        // moves to a new location, and then changes the device orientation, the original location
        // is displayed as the activity is re-created.
        if (mCurrentLocation == null) {
            if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
            updateLocationUI();
        }
    }
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_ACCESS_LOCATION: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    updateLocationUI();

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                } else {

                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(TAG, "Connection suspended");
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
        mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
        updateLocationUI();
    }
    /**
     * Stores activity data in the Bundle.
     */
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putBoolean(KEY_REQUESTING_LOCATION_UPDATES, mRequestingLocationUpdates);
        savedInstanceState.putParcelable(KEY_LOCATION, mCurrentLocation);
        savedInstanceState.putString(KEY_LAST_UPDATED_TIME_STRING, mLastUpdateTime);
        super.onSaveInstanceState(savedInstanceState);
    }
}

These are permissions which I have added in manifest file

<uses-permission android:name = "android.permission.INTERNET" />
<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION" />


This is the xml code for latitude and longitude

<TextView
        android:id="@+id/myLatitude"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:textColor="@color/cordinates_color"
        />
    <TextView
        android:id="@+id/myLongitude"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="15sp"
        android:textColor="@color/cordinates_color"
        />

I have added this line in build.gradle app level and sync the project compile 'com.google.android.gms:play-services:10.0.1'

I have been able to connect to GoogleApiClient but coordinates are not coming up. This is a glimpse of logcat

01-29 14:49:31.395 10560-10560/com.example.luke.xyz I/EditInformation: Building GoogleApiClient
01-29 14:49:31.675 10560-10560/com.example.luke.xyz D/ViewRootImpl: #1 mView = android.widget.LinearLayout{2782c80 V.E...... ......I. 0,0-0,0}
01-29 14:49:31.715 10560-10616/com.example.luke.xyz D/mali_winsys: new_window_surface returns 0x3000,  [468x91]-format:1
01-29 14:49:31.755 10560-10560/com.example.luke.xyz I/EditInformation: Connected to GoogleApiClient
01-29 14:49:31.805 10560-10616/com.example.luke.xyz V/RenderScript: 0xdc179000 Launching thread(s), CPUs 8

I am testing it on Android Marshmallow. Still unable to figure it out.
Can you help me out ?
Thank you

5 Answers5

1

Ask run time permissions like this:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.ACCESS_COURSE_L‌​OCATION)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.ACCESS_COURSE_L‌​OCATION)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.ACCESS_COURSE_L‌​OCATION},
                MY_PERMISSIONS_REQUEST_LOCATION);

        // MY_PERMISSIONS_REQUEST_LOCATION is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Then:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

more details you can find it out here: https://developer.android.com/training/permissions/requesting.html

Pushpendra
  • 2,791
  • 4
  • 26
  • 49
0

Go to Settings > Apps > Your_app, and in the Permissions tab, enable Location for instantly getting the output on Marshmallow.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Avinash Roy
  • 953
  • 1
  • 8
  • 25
0

Use both the location Permission - ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION, same time use Double for LatLng:

@Override
public void onConnected(@Nullable Bundle connectionHint) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        if (ActivityCompat.checkSelfPermission
                (getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                &&
                ActivityCompat.checkSelfPermission
                        (getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 1);
            return;

        }
    }
    else{
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if(mLastLocation!=null){
        mLatitudeText.setText(Double.parseDouble(mLastLocation.getLatitude()));
        mLongitudeText.setText(Double.parseDouble(mLastLocation.getLongitude()));
        }else {
              Toast.makeText(getContext(),"LOcation is null",Toast.LENGTH_SHORT).show();
              }

    }

}

Handle requested Permission:

  @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {

        case 1:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                Toast.makeText(getContext(),"PERMISSION_GRANTED",Toast.LENGTH_SHORT).show();
              }
              else {
                    Toast.makeText(getContext(),"PERMISSION_GRANTED",Toast.LENGTH_SHORT).show();
              }
           break;
       }
  }
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • Still no help!! –  Jan 12 '17 at 13:28
  • Should I include these lines of code in onRequestPermissionResult in else statement ? Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); if(mLastLocation!=null){ mLatitudeText.setText(Double.parseDouble(mLastLocation.getLatitude())); mLongitudeText.setText(Double.parseDouble(mLastLocation.getLongitude())); } –  Jan 12 '17 at 13:31
  • I am getting error on line containing this I have changed it to getContext(), And I think I don't need to convert it to double since I am taking values in String like this String.valueOf(mLastLocation.getLatitude()); I hope this will work. Right ? –  Jan 12 '17 at 13:51
  • I am still unable to retrieve the coordinates. I am updating my question and source code once again. –  Jan 12 '17 at 15:36
  • please take a look –  Jan 12 '17 at 15:42
  • I am testing it on android lollipop I hope that's not gonna make any difference. Right ? –  Jan 12 '17 at 15:46
  • I have tried a lot but still unable to retrieve the coordinates. I have sync my gradle to play-services and put this line in build.gradle app level compile 'com.google.android.gms:play-services-location:10.0.1' but still nothing –  Jan 13 '17 at 01:34
  • Will the old way run on latest android ? –  Jan 13 '17 at 07:36
  • when i am running the apk it's not showing the message Location is null like you used in toast. –  Jan 13 '17 at 07:38
  • come to chat room –  Jan 13 '17 at 09:38
  • yea bro !! still unable to figure it out. Tried everything even the link you shared –  Jan 14 '17 at 07:05
  • I have to because if didn't there is no point of editing the question. –  Jan 14 '17 at 07:05
0

Your coding is fine you just need to add few more permission to your manifest file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.LOCATION_HARDWARE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


Make sure your gps is enabled. Let me know if this fix your problem.

Lokesh Pandey
  • 1,739
  • 23
  • 50
  • how does LOCATION_HARDWARE permission help us to grab user location ? –  Jan 14 '17 at 07:42
  • It allows an application to use location features in hardware, such as the geofencing api. Follow this link for more information https://developer.android.com/reference/android/Manifest.permission.html#LOCATION_HARDWARE – Lokesh Pandey Jan 14 '17 at 07:44
0

You are currently only asking for the last known location in onConnected. If there is no cached location, this will do nothing.

You should override onLocationChanged to receive the current location once your Google API client is connected. https://developer.android.com/training/location/receive-location-updates.html#callback


The GoogleApiClient.connect method is asynchronous. In the documentation I linked above, the request for location updates is made after the GoogleApiClient connects. You are making this request in onResume, but you are not guaranteed that the GoogleApiClient is connected until onConnected. Make the request in onConnected, as the example in the documentation shows.

mattm
  • 5,851
  • 11
  • 47
  • 77
  • hi I am still unable to grab the coordinates. I have gone through entire documentation. And updated my code. Can you help me out here. –  Jan 28 '17 at 03:48
  • This is strange I have updated the code, I tested the same code in onCreate() method it displays the coordinates but it didn't work in onCreateView() method. I don't think that's gonna make any difference according to this answer http://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra right ? –  Jan 29 '17 at 09:41