Use Android Location to get the current location

  • 2020-05-09 19:19:04
  • OfStack

In the application of Android, there is often a need to get the current geographical location. For example, WeChat needs to get the user's current location to get the people nearby.


public Location getLocation() {//  To obtain Location through LocationManger To get! 
  LocationManager locManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  Location loc = locManger.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  if (loc == null) {
   loc = locManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  }
  return loc;
 }
public void LocationMethod() {// Location Introduction to common methods 
  Location loc = getLocation();
  // loc.distanceTo(Location dest);float
  // loc.getAltitude();double  The altitude 
  // loc.getLatitude();double
  // loc.getLongitude();double
  // loc.getSpeed();float
 }

To use the Location service, you must also add the permissions of Location: < uses-permission   android:name="android.permission.ACCESS_FINE_LOCATION"/ >  

OK, the content of this article is relatively simple!


Related articles: