Location

Project
 avatar
unknown
c_cpp
4 years ago
3.5 kB
5
Indexable
package com.example.cse332mainproject;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import static androidx.constraintlayout.motion.widget.Debug.getLocation;

public class HomepageActivity extends AppCompatActivity {

    Button btlocation;
    TextView textView1, textView2, textView3, textView4, textView5;
    FusedLocationProviderClient fusedlocationproviderclient;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);

        btlocation = findViewById(R.id.btlocation);
        textView1 = findViewById(R.id.textView1);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        textView4 = findViewById(R.id.textView4);
        textView5 = findViewById(R.id.textView5);

        fusedlocationproviderclient = LocationServices.getFusedLocationProviderClient(this);
        btlocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED)
                {
                    getLocation();
                }
                else
                {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
                }

            }

        });


    }

       private void getLocation()
       {
           fusedlocationproviderclient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
               @Override
               public void onComplete(@NonNull @org.jetbrains.annotations.NotNull Task<Location> task) {
                   Location location = task.getResult();
                   if(location != null)
                   {
                      
                       try {
                           Geocoder geocoder = new Geocoder(MainActivity.this,
                                   Locale.getDefault());
                           List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                       } catch (IOException e) {
                           e.printStackTrace();
                       }


                   }
               }
           })
       }

    public void gotoSettings(View view) {
        Intent i = new Intent(this,settingActivity.class);
        startActivity(i);
    }

    public void gotoShop(View view) {
        Intent i = new Intent(this,shopActivity.class);
        startActivity(i);
    }

}
Editor is loading...