From fa1cf3e516f5cc1b443dbfb4286c327d368a34e0 Mon Sep 17 00:00:00 2001 From: Aravind Date: Wed, 14 Nov 2018 22:02:36 +0530 Subject: [PATCH] Added Address --- .../aravind/getlocation/GeoLocator.java | 109 ++++++++++++++++++ .../aravind/getlocationmain/MainActivity.java | 20 ++-- 2 files changed, 117 insertions(+), 12 deletions(-) diff --git a/GeoLocator/src/main/java/com/location/aravind/getlocation/GeoLocator.java b/GeoLocator/src/main/java/com/location/aravind/getlocation/GeoLocator.java index 1ca2a01..f754005 100644 --- a/GeoLocator/src/main/java/com/location/aravind/getlocation/GeoLocator.java +++ b/GeoLocator/src/main/java/com/location/aravind/getlocation/GeoLocator.java @@ -3,12 +3,22 @@ import android.Manifest; import android.app.Activity; import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; import android.content.pm.PackageManager; +import android.location.Address; +import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; +import android.provider.Settings; import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AlertDialog; import android.widget.Toast; +import java.io.IOException; +import java.util.List; +import java.util.Locale; + public class GeoLocator { @@ -16,6 +26,7 @@ public class GeoLocator { private static final int REQUEST_LOCATION = 1; private Context context; private Activity activity; + private String address,city,state,country,postalCode,knownName; @@ -25,6 +36,8 @@ public GeoLocator(Context context, Activity activity) { GetLocation(); + geoAddress(); + } public void GetLocation(){ @@ -36,6 +49,7 @@ public void GetLocation(){ if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { + showSettingsAlert(); Toast.makeText(context,"Permission Denied",Toast.LENGTH_SHORT).show(); } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){ @@ -110,4 +124,99 @@ public double getLongitude() { + + public void geoAddress(){ + + + Geocoder geocoder; + List
addresses; + geocoder = new Geocoder(context, Locale.getDefault()); + + try { + addresses = geocoder.getFromLocation(lattitude, longitude, 1); + + address = addresses.get(0).getAddressLine(0); + city = addresses.get(0).getLocality(); + state = addresses.get(0).getAdminArea(); + country = addresses.get(0).getCountryName(); + postalCode = addresses.get(0).getPostalCode(); + knownName = addresses.get(0).getFeatureName(); + + + + + } catch (IOException e) { + e.printStackTrace(); + } + + + + + + } + + + + public String getAddress() { + return address; + } + + public String getCity() { + return city; + } + + public String getState() { + return state; + } + + public String getCountry() { + return country; + } + + public String getPostalCode() { + return postalCode; + } + + public String getKnownName() { + return knownName; + } + + + + + + + public void showSettingsAlert(){ + AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); + + // Setting Dialog Title + alertDialog.setTitle("GPS is settings"); + + // Setting Dialog Message + alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); + + // On pressing Settings button + alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog,int which) { + Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + context.startActivity(intent); + } + }); + + // on pressing cancel button + alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + // Showing Alert Message + alertDialog.show(); + } + + + + + + } diff --git a/app/src/main/java/com/location/aravind/getlocationmain/MainActivity.java b/app/src/main/java/com/location/aravind/getlocationmain/MainActivity.java index 99ecc86..59d3913 100644 --- a/app/src/main/java/com/location/aravind/getlocationmain/MainActivity.java +++ b/app/src/main/java/com/location/aravind/getlocationmain/MainActivity.java @@ -1,5 +1,8 @@ package com.location.aravind.getlocationmain; +import android.location.Address; +import android.location.Geocoder; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; @@ -8,6 +11,8 @@ import com.location.aravind.getlocation.GeoLocator; +import java.util.List; + public class MainActivity extends AppCompatActivity { TextView tvValue; @@ -26,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) { public void onClick(View v) { GeoLocator geoLocator = new GeoLocator(getApplicationContext(),MainActivity.this); - tvValue.setText( geoLocator.getLattitude()+","+ geoLocator.getLongitude()); + tvValue.setText( geoLocator.getLattitude()+","+ geoLocator.getLongitude() + "\n" + geoLocator.getAddress()); } }); @@ -50,19 +55,10 @@ public void initWidgets(){ } -/* - Geocoder geocoder; - List
addresses; - geocoder = new Geocoder(this, Locale.getDefault()); - addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5 - String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() - String city = addresses.get(0).getLocality(); - String state = addresses.get(0).getAdminArea(); - String country = addresses.get(0).getCountryName(); - String postalCode = addresses.get(0).getPostalCode(); - String knownName = addresses.get(0).getFeatureName();*/ + +