How to Use Android Location to obtain the current geographic Location

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.