Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this blog, I have shown how to fetch your current location using the older Apis. Google introduce the new API on the basis of the google play services.
For any location based application, this functionality is very use full to your user. So there are few steps to fetch your location in android.
Step 1: First all create a class which extends the Service class and implements the LocationListener,
public class MyGPSTracker extends Service implements LocationListener { public MyGPSTracker(Context context) { this.mContext = context; getLocation(); } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } 123456789101112131415161718192021222324252627282930313233 public class MyGPSTracker extends Service implements LocationListener { public MyGPSTracker(Context context) { this.mContext = context; getLocation(); } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } @Nullable @Override public IBinder onBind(Intent intent) { return null; }}
Step 2: Then create the Location instance and try to get the latitude and longitude by the Location manager,
Location location; protected LocationManager locationManager; double latitude; // latitude double longitude; // longitude 12345 Location location;protected LocationManager locationManager; double latitude; // latitudedouble longitude; // longitude
Create a method which returns the location instance with getting the lat-long,
public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 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 lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 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; } 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status isNetworkEnabled = locationManager .isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 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 lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 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; }
Step 3: Create the function to getting the Latitude and Longitude,
public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitude return latitude; } public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitude return longitude; } 1234567891011121314151617 public double getLatitude() { if (location != null) { latitude = location.getLatitude(); } // return latitude return latitude; } public double getLongitude() { if (location != null) { longitude = location.getLongitude(); } // return longitude return longitude; }
Step 4: Create another class from where to get the location,
public void getYourLocation(View v) { // permission check in Marshmallow if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } else { gps = new MyGPSTracker(MyLocationClass.this); // check if GPS enabled if (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); Log.d("latitude",latitude + ""); Log.d("longitude",longitude + ""); } else { // can't get location // GPS or Network is not enabled // Ask user to enable GPS/network in settings gps.showSettingsAlert(); } } } 1234567891011121314151617181920212223242526 public void getYourLocation(View v) { // permission check in Marshmallow if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } else { gps = new MyGPSTracker(MyLocationClass.this); // check if GPS enabled if (gps.canGetLocation()) { latitude = gps.getLatitude(); longitude = gps.getLongitude(); Log.d("latitude",latitude + ""); Log.d("longitude",longitude + ""); } else { // can't get location // GPS or Network is not enabled // Ask user to enable GPS/network in settings gps.showSettingsAlert(); } } }
Step 5: After successfully getting the lat-long, we can get the user current location by Reverse Geocoding
try { geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); addresses = geocoder.getFromLocation(latitude, longitude, 1); String address = addresses.get(0).getAddressLine(0); String city = addresses.get(0).getLocality(); state = addresses.get(0).getAdminArea(); country = addresses.get(0).getCountryName(); String postalCode = addresses.get(0).getPostalCode(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } 12345678910111213 try { geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); addresses = geocoder.getFromLocation(latitude, longitude, 1); String address = addresses.get(0).getAddressLine(0); String city = addresses.get(0).getLocality(); state = addresses.get(0).getAdminArea(); country = addresses.get(0).getCountryName(); String postalCode = addresses.get(0).getPostalCode(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } Note: Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.
Note: Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.
Source: http://www.androidhive.info/
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
The results provided were above our expectations both in deliverability timeline as well as operation. They are a top-notch company with many resources and a company we feel honoured to work with again!
Lee Seward
Founder, Gro-Connect
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.