Obtain the location information of the Android mobile phone

Source: Internet
Author: User

There are three sources of positioning information on Android phones: GPS, cell-ID, and WiFi. the latter two types are based on the network, so they are centrally located based on the network in the android SDK. Currently, the three positioning methods are not perfect, and each has its own advantages and disadvantages.

GPS signal-based positioning has a high accuracy among the three, but the disadvantage is that the current civil GPS signal is poor in the building, and the start time of the GPS module is also slow, the working principle of GPS is actually to search for four nearby satellites and locate them based on the distance from the current location to the four satellites. The GPS module finds four satellites (also called the GPS startup process) it may take several minutes depending on the signal strength and region. The Positioning Based on the base station and WiFi is called Network Positioning in the android API, and developers cannot distinguish the two, they feature high speed, but the accuracy is not as high as that of GPS. We also often hear that the agps locating algorithm combines the advantages of the two algorithms. First, we can obtain a rough location based on the Fast Location of the network, then, the GPS module can quickly search for nearby Satellites Based on the location. This is the current Mobile Phone Positioning Method with better results. The following describes how to perform basic GPS and network positioning.

You need to obtain the locationmanager before starting locating, and then select a locating method based on certain rules. The Code is as follows,

Locationmanager = (locationmanager) getsystemservice (location_service );

Select the rule of the Locating Algorithm:

Criteria = new criteria ();
Criteria. setaccuracy (criteria. accuracy_fine); // you can specify the maximum precision.
Criteria. setaltituderequired (false); // do not require elevation information criteria. setcostallowed (true); // whether to allow payment
Criteria. setpowerrequirement (criteria. power_low); // power requirements
Criteria. setbearingrequired (false); // bearing information is not required

For more information about bearing, see bearing.
 

// Select a locating method based on the above rules

Bestprovider = locationmanager. getbestprovider (criteria, false );

// Generally, to improve the user experience, we first cache the result of the previous location,

// Open the last location result at startup, which is called last known location.

Location = locationmanager. getlastknownlocation (bestprovider );

// Next, we start location update in the onresume function on the activity display interface.

Locationmanager. requestlocationupdates (bestprovider, 2000, 1, this );

// And disable it in the onpause Function

Locationmanager. removeupdates (this );

// Implement the locationlistener port for the current activity and obtain the location information in the onlocationchanged function.

Public void onlocationchanged (location ){

Log. D (TAG, "latitude:" + location. getlatitude ());

Log. D (TAG, "longpolling:" + location. getlongpolling ());

}

// Recently remember to grant the positioning permission to the androidmenifest. xml file.

<Uses-Permission Android: Name = "android. Permission. access_coarse_location"/>
<Uses-Permission Android: Name = "android. Permission. access_fine_location"/>


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.