Skip to content

Commit

Permalink
Added Address
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindVijay7 committed Nov 14, 2018
1 parent de30752 commit fa1cf3e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@
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 {


private static double lattitude ,longitude;
private static final int REQUEST_LOCATION = 1;
private Context context;
private Activity activity;
private String address,city,state,country,postalCode,knownName;



Expand All @@ -25,6 +36,8 @@ public GeoLocator(Context context, Activity activity) {

GetLocation();

geoAddress();

}

public void GetLocation(){
Expand All @@ -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)){
Expand Down Expand Up @@ -110,4 +124,99 @@ public double getLongitude() {




public void geoAddress(){


Geocoder geocoder;
List<Address> 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();
}






}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,6 +11,8 @@

import com.location.aravind.getlocation.GeoLocator;

import java.util.List;

public class MainActivity extends AppCompatActivity {

TextView tvValue;
Expand All @@ -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());

}
});
Expand All @@ -50,19 +55,10 @@ public void initWidgets(){
}


/*
Geocoder geocoder;
List<Address> 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();*/





Expand Down

0 comments on commit fa1cf3e

Please sign in to comment.