Skip to content

Instantly share code, notes, and snippets.

@AkaashSaini
Created July 7, 2021 12:44
Show Gist options
  • Save AkaashSaini/38ccdafd0fec70f3650b94194b0a10d9 to your computer and use it in GitHub Desktop.
Save AkaashSaini/38ccdafd0fec70f3650b94194b0a10d9 to your computer and use it in GitHub Desktop.
->activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="30dp"
android:text="HIKER'S WATCH"
android:textColor="@android:color/white"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="@+id/lattitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Lattitude : "
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/longitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Longitude : "
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/altitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Altitude : "
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/accuracy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Accuracy : "
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Address : "
android:textColor="@android:color/white"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
->in Manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
->in MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final int LOCATION_REQUEST_CODE = 101;
public static final int REQUEST_CHECK_SETTINGS = 0;
TextView lat,lon,alt,acu,add;
LocationManager manager;
LocationListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lat=findViewById(R.id.lattitude);
lon=findViewById(R.id.longitude);
alt=findViewById(R.id.altitude);
acu=findViewById(R.id.accuracy);
add=findViewById(R.id.address);
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(10000);
locationRequest.setFastestInterval(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
Log.i("clash", "onSuccess: ");
lat.setText( "Latitude : please wait..");
lon.setText( "Longitude : please wait..");
alt.setText( "Altitude : please wait..");
acu.setText( "Accuracy : please wait..");
add.setText("Address : please wait..");
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
Log.i("clash", "onFailure: ");
lat.setText( "Latitude : turn on gps");
lon.setText( "Longitude : turn on gps");
alt.setText( "Altitude : turn on gps");
acu.setText( "Accuracy : turn on gps");
add.setText("Address : turn on gps");
}
}
});
manager= (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
listener=new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
//Log.i("clash", "onLocationChanged: "+location);
lat.setText( "Latitude : "+ location.getLatitude());
lon.setText( "Longitude : "+ location.getLongitude());
alt.setText( "Altitude : "+ location.getAltitude());
acu.setText( "Accuracy : "+ location.getAccuracy());
Geocoder geocoder=new Geocoder(MainActivity.this, Locale.getDefault());
List<Address> addressList=null;
String address=null;
try {
addressList=geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
if (addressList.size()>0 && addressList!=null){
if (addressList.get(0).getAddressLine(0)!=null){
address=addressList.get(0).getAddressLine(0);
}
else{
address=addressList.get(0).toString();
}
}
else{
address="Unable to get Location";
}
} catch (Exception e) {
e.printStackTrace();
}
add.setText("Address : "+address);
}
@Override
public void onProviderEnabled(@NonNull String provider) { }
@Override
public void onProviderDisabled(@NonNull String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
}
else{
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,listener);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_CHECK_SETTINGS){
if (resultCode==RESULT_OK){
lat.setText( "Latitude : please wait..");
lon.setText( "Longitude : please wait..");
alt.setText( "Altitude : please wait..");
acu.setText( "Accuracy : please wait..");
add.setText("Address : please wait..");
}
else{
lat.setText( "Latitude : turn on gps");
lon.setText( "Longitude : turn on gps");
alt.setText( "Altitude : turn on gps");
acu.setText( "Accuracy : turn on gps");
add.setText("Address : turn on gps");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment