Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.maxet24.chargely;
- import android.Manifest;
- import android.location.Location;
- import android.os.Bundle;
- import androidx.core.app.ActivityCompat;
- import androidx.fragment.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import com.google.android.gms.location.FusedLocationProviderClient;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.tasks.OnSuccessListener;
- public class ListFragment extends Fragment {
- private TextView longText;
- private TextView latText;
- private FusedLocationProviderClient fusedLocationClient;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.fragment_list, container, false);
- // Init TextView
- longText = (TextView) v.findViewById(R.id.longTest);
- latText = (TextView) v.findViewById(R.id.latTest);
- // Get Location according to guide
- fusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
- fusedLocationClient.getLastLocation()
- .addOnSuccessListener(this, new OnSuccessListener<Location>() {
- @Override
- public void onSuccess(Location location) {
- // Got last known location. In some rare situations this can be null.
- if (location != null) {
- longText.setText("Longitude: " + Double.toString(location.getLongitude()));
- latText.setText("latitude: " + Double.toString(location.getLatitude()));
- }
- }
- });
- return v;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment