In Android applications, you often need to obtain the current geographical location. For example, to obtain a person nearby, you need to obtain the current location of the user.
Copy codeThe Code is as follows: public Location getLocation () {// get Location through LocationManger!
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 () {// introduction to common Location methods
Location loc = getLocation ();
// Loc. distanceTo (Location dest); float
// Loc. getAltitude (); double altitude
// Loc. getLatitude (); double
// Loc. getlongparts (); double
// Loc. getSpeed (); float
}
To use the Location service, you must also add the Location permission: <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
OK. The content of this article is relatively simple!