Put this code inside onCreate
Friday, 14 July 2017
Sunday, 9 July 2017
How to convert GPS Coordinates ( Latitude and Longitude ) to Address in Android.
Manifest
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
activity.gps.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="raj.sujeet.com.example.GPSActivity"android:weightSum="1"><Buttonandroid:id="@+id/getLL"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="get Lati/long"tools:layout_editor_absoluteX="39dp"tools:layout_editor_absoluteY="28dp"android:layout_marginTop="10dp"tools:ignore="MissingConstraints"android:layout_weight="0.01" /><EditTextandroid:id="@+id/latitude"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /> <EditTextandroid:id="@+id/longitude"android:layout_marginTop="10dp"android:layout_width="match_parent"android:layout_height="wrap_content" /> <Buttonandroid:id="@+id/getLocation"android:layout_marginTop="20dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="get location" /> <TextViewandroid:layout_margin="10dp"android:id="@+id/result"android:layout_width="wrap_content"android:layout_height="wrap_content" /> </LinearLayout>GPSActivity.javapublic class GPSActivity extends AppCompatActivity { Button getLL,getlocation; EditText latitudeLL,longitudeLL; String lati,longi; TextView result; Geocoder geocoder; List<Address> addressList; // GPSTracker class GPSTracker gps; Context mContext; double latitude,longitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gps); result = (TextView)findViewById(R.id.result); geocoder = new Geocoder(this, Locale.getDefault()); latitudeLL = (EditText)findViewById(R.id.latitude); longitudeLL = (EditText)findViewById(R.id.longitude); getlocation = (Button)findViewById(R.id.getLocation); mContext = this; getLL = (Button)findViewById(R.id.getLL); getLL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(GPSActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { Toast.makeText(mContext, "You need have granted permission",Toast.LENGTH_SHORT).show(); gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabledif (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); // \n is for new lineToast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); lati = Double.toString(latitude); longi = Double.toString(longitude); latitudeLL.setText(lati); longitudeLL.setText(longi); } else { // Can't get location.// GPS or network is not enabled.// Ask user to enable GPS/network in settings.gps.showSettingsAlert(); } } } }); getlocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { addressList = geocoder.getFromLocation(latitude,longitude,1); String addressStr = addressList.get(0).getAddressLine(0); String areaStr = addressList.get(0).getLocality(); String cityStr = addressList.get(0).getAdminArea(); String countryStr = addressList.get(0).getCountryName(); String postalcodeStr = addressList.get(0).getPostalCode(); String fullAddress = addressStr+", "+areaStr+", "+cityStr+", "+countryStr+", "+postalcodeStr; result.setText(fullAddress); } catch (IOException e) { e.printStackTrace(); } } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty.if (grantResults.length > 0&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabledif (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new lineToast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else { // Can't get location.// GPS or network is not enabled.// Ask user to enable GPS/network in settings.gps.showSettingsAlert(); } } else { // permission denied, boo! Disable the// functionality that depends on this permission. Toast.makeText(mContext, "You need to grant permission",Toast.LENGTH_SHORT).show(); } return; } } } }GPSTracker.javapackage raj.sujeet.com.example; import android.Manifest; import android.app.Activity; import android.app.Service; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.util.Log; /** * Created by Sujeet Raj on 09-07-2017. */public class GPSTracker extends Service { private Context mContext; // Flag for GPS statusboolean isGPSEnabled = false; // Flag for network statusboolean isNetworkEnabled = false; // Flag for GPS statusboolean canGetLocation = false; Location location;// Locationdouble latitude;// Latitudedouble longitude;// Longitude // The minimum distance to change Updates in metersprivate static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;// 10 meters // The minimum time between updates in millisecondsprivate static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;// 1 minute // Declaring a Location Manager protected LocationManager locationManager; Activity activity; public GPSTracker() { } public GPSTracker(Context context, Activity activity) { this.mContext = context; this.activity = activity; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // Getting GPS statusisGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // Getting network statusisNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // No network provider is enabled} else { this.canGetLocation = true; if (isNetworkEnabled) { int requestPermissionsCode = 50; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // If GPS enabled, get latitude/longitude using GPS Servicesif (isGPSEnabled) { if (location == null) { if (ContextCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50); } else { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener* Calling this function will stop using GPS in your app.*/public void stopUsingGPS() { } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; /*** Function to get latitude*/public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitudereturn latitude; } /** * Function to get longitude*/public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitudereturn longitude; } /** * Function to check GPS/Wi-Fi enabled ** @return boolean */public boolean canGetLocation() { return this.canGetLocation; } /** * Function to show settings alert dialog.* On pressing the Settings button it will launch Settings Options. */public void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog TitlealertDialog.setTitle("GPS is settings"); // Setting Dialog MessagealertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing the Settings button.alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // On pressing the cancel buttonalertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert MessagealertDialog.show(); } @Override public IBinder onBind(Intent arg0) { return null; } }
How to get Latitude and Longitude in Android Device 2017.
activity_gps.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="raj.sujeet.com.example.GPSActivity">
<Button android:id="@+id/getLL" android:layout_width="311dp" android:layout_height="51dp" android:text="get Lati/long" tools:layout_editor_absoluteX="39dp" tools:layout_editor_absoluteY="28dp" tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="raj.sujeet.com.example.GPSActivity">
<Button android:id="@+id/getLL" android:layout_width="311dp" android:layout_height="51dp" android:text="get Lati/long" tools:layout_editor_absoluteX="39dp" tools:layout_editor_absoluteY="28dp" tools:ignore="MissingConstraints" />
</android.support.constraint.ConstraintLayout>
GPSActivity.java
public class GPSActivity extends AppCompatActivity { Button getLL; // GPSTracker class GPSTracker gps; Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gps); mContext = this; getLL = (Button)findViewById(R.id.getLL); getLL.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(GPSActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { Toast.makeText(mContext, "You need have granted permission", Toast.LENGTH_SHORT).show(); gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabled&& ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(GPSActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { Toast.makeText(mContext, "You need have granted permission", Toast.LENGTH_SHORT).show(); gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabled
if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new lineif (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else {Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else {
// Can't get location.// Can't get location.
// GPS or network is not enabled.// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.// Ask user to enable GPS/network in settings.
gps.showSettingsAlert(); } } } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty.gps.showSettingsAlert(); } } } }); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty.
if (grantResults.length > 0if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabled&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. gps = new GPSTracker(mContext, GPSActivity.this); // Check if GPS enabled
if (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new lineif (gps.canGetLocation()) { double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); // \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else { // Can't get location.Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); } else { // Can't get location.
// GPS or network is not enabled.// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.// Ask user to enable GPS/network in settings.
gps.showSettingsAlert(); } } else { // permission denied, boo! Disable thegps.showSettingsAlert(); } } else { // permission denied, boo! Disable the
// functionality that depends on this permission. Toast.makeText(mContext, "You need to grant permission", Toast.LENGTH_SHORT).show(); } return; } } }// functionality that depends on this permission. Toast.makeText(mContext, "You need to grant permission", Toast.LENGTH_SHORT).show(); } return; } } }GPSTracker.javaManifest
public class GPSTracker extends Service { private Context mContext; // Flag for GPS statuspublic class GPSTracker extends Service { private Context mContext; // Flag for GPS statusboolean isGPSEnabled = false; // Flag for network statusboolean isNetworkEnabled = false; // Flag for GPS statusboolean canGetLocation = false; Location location;// Locationdouble latitude;// Latitudedouble longitude;// Longitude // The minimum distance to change Updates in metersprivate static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;// 10 meters // The minimum time between updates in millisecondsprivate static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;// 1 minute // Declaring a Location Managerprotected LocationManager locationManager; Activity activity; public GPSTracker() { } public GPSTracker(Context context, Activity activity) { this.mContext = context; this.activity = activity; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // Getting GPS statusisGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // Getting network statusisNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // No network provider is enabled} else { this.canGetLocation = true; if (isNetworkEnabled) { int requestPermissionsCode = 50; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // If GPS enabled, get latitude/longitude using GPS Servicesif (isGPSEnabled) { if (location == null) { if (ContextCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(activity,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50); } else { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener* Calling this function will stop using GPS in your app. */public void stopUsingGPS() { } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } @Override public void onStatusChanged(String provider,int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; /** * Function to get latitude */public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitudereturn latitude; } /** * Function to get longitude */public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitudereturn longitude; } /** * Function to check GPS/Wi-Fi enabled** @return boolean */public boolean canGetLocation() { return this.canGetLocation; } /*** Function to show settings alert dialog.* On pressing the Settings button it will launch Settings Options. */public void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog TitlealertDialog.setTitle("GPS is settings"); // Setting Dialog MessagealertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing the Settings button.alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // On pressing the cancel buttonalertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert MessagealertDialog.show(); } @Override public IBinder onBind(Intent arg0) { return null; } }
boolean isGPSEnabled = false; // Flag for network statusboolean isGPSEnabled = false; // Flag for network status
boolean isNetworkEnabled = false; // Flag for GPS statusboolean isNetworkEnabled = false; // Flag for GPS status
boolean canGetLocation = false; Location location;boolean canGetLocation = false; Location location;
// Location// Location
double latitude;double latitude;
// Latitude// Latitude
double longitude;double longitude;
// Longitude // The minimum distance to change Updates in meters// Longitude // The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1000;
// 10 meters // The minimum time between updates in milliseconds// 10 meters // The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1;
// 1 minute // Declaring a Location Manager// 1 minute // Declaring a Location Manager
protected LocationManager locationManager; Activity activity; public GPSTracker() { } public GPSTracker(Context context, Activity activity) { this.mContext = context; this.activity = activity; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // Getting GPS statusprotected LocationManager locationManager; Activity activity; public GPSTracker() { } public GPSTracker(Context context, Activity activity) { this.mContext = context; this.activity = activity; getLocation(); } public Location getLocation() { try { locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE); // Getting GPS status
isGPSEnabled = locationManagerisGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER); // Getting network status.isProviderEnabled(LocationManager.GPS_PROVIDER); // Getting network status
isNetworkEnabled = locationManagerisNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // No network provider is enabled.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // No network provider is enabled
} else { this.canGetLocation = true; if (isNetworkEnabled) { int requestPermissionsCode = 50; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager} else { this.canGetLocation = true; if (isNetworkEnabled) { int requestPermissionsCode = 50; locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("Network", "Network"); if (locationManager != null) { location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // If GPS enabled, get latitude/longitude using GPS Services.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } // If GPS enabled, get latitude/longitude using GPS Services
if (isGPSEnabled) { if (location == null) { if (ContextCompat.checkSelfPermission(activity,if (isGPSEnabled) { if (location == null) { if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTEDManifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(activity,&& ActivityCompat.checkSelfPermission(activity,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50); } else { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManagernew String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50); } else { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, mLocationListener); Log.d("GPS Enabled", "GPS Enabled"); if (locationManager != null) { location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printStackTrace(); } return location; } /** * Stop using GPS listener
* Calling this function will stop using GPS in your app. */* Calling this function will stop using GPS in your app. */
public void stopUsingGPS() { } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } @Override public void onStatusChanged(String provider,public void stopUsingGPS() { } private final LocationListener mLocationListener = new LocationListener() { @Override public void onLocationChanged(final Location location) { if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } @Override public void onStatusChanged(String provider,
int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; /** * Function to get latitude */int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }; /** * Function to get latitude */
public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitudepublic double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitude
return latitude; } /** * Function to get longitude */return latitude; } /** * Function to get longitude */
public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitudepublic double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitude
return longitude; } /** * Function to check GPS/Wi-Fi enabledreturn longitude; } /** * Function to check GPS/Wi-Fi enabled
**
* @return boolean */* @return boolean */
public boolean canGetLocation() { return this.canGetLocation; } /**public boolean canGetLocation() { return this.canGetLocation; } /**
* Function to show settings alert dialog.* Function to show settings alert dialog.
* On pressing the Settings button it will launch Settings Options. */* On pressing the Settings button it will launch Settings Options. */
public void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Titlepublic void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title
alertDialog.setTitle("GPS is settings"); // Setting Dialog MessagealertDialog.setTitle("GPS is settings"); // Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing the Settings button.alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?"); // On pressing the Settings button.
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // On pressing the cancel buttonalertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } }); // On pressing the cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert MessagealertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); // Showing Alert Message
alertDialog.show(); } @Override public IBinder onBind(Intent arg0) { return null; } }alertDialog.show(); } @Override public IBinder onBind(Intent arg0) { return null; } }
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Subscribe to:
Comments (Atom)