Android uses GPS to locate the longitude and latitude

Source: Internet
Author: User

Mobile is the biggest feature of mobile phones and handheld devices. You can use the ddms view of eclipse to simulate device location changes, change the longitude and latitude, and click send to runProgramIn the application, dynamically obtain the device location, and then display the current location information.

There are three steps to obtain location information:

1. Add system permissions to support access to LBs hardware

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

</Uses-Permission>

2. Obtain the locationmanager object of the system service.

Locationmanager loctionmanager;

String contextservice = context. location_service;

// Obtain the locationmanager object through system services

Loctionmanager = (locationmanager) getsystemservice (contextservice );

3. obtain the location provider and obtain the location information through the location provider. You can specify a specific location provider or provide a standard set, allows the system to match the most suitable location provider according to the standard. Location Information is provided by location.

A. Obtain the location through the GPS location provider (specify the specific location provider)

String provider = locationmanager. gps_provider;

Location = loctionmanager. getlastknownlocation (provider );

B. Use a standard set to enable the system to automatically select the available best location provider and provide the location

Criteria = new criteria ();

Criteria. setaccuracy (criteria. accuracy_fine); // High Precision

Criteria. setaltituderequired (false); // altitude not required

Criteria. setbearingrequired (false); // orientation not required

Criteria. setcostallowed (true); // cost allowed

Criteria. setpowerrequirement (criteria. power_low); // Low Power Consumption

// From the available locations, the best providers' matching the above standards

String provider = loctionmanager. getbestprovider (criteria, true );

// Obtain the location of the last change

Location = loctionmanager. getlastknownlocation (provider );

Finally, the location information is displayed in textview,

Changes in listening location

// Listen for location changes, once every 2 seconds, more than 10 meters away

Loctionmanager. requestlocationupdates (provider, 2000, 10, locationlistener );

// Location listener

Private Final locationlistener = new locationlistener (){

@ Override

Public void onstatuschanged (string provider, int status, bundle extras ){

}

@ Override

Public void onproviderenabled (string provider ){

}

@ Override

Public void onproviderdisabled (string provider ){

}

// Triggered when the position changes

@ Override

Public void onlocationchanged (location ){

// Update textview display with the new location

Updatewithnewlocation (location );

}

};

By changing the location longitude and latitude, the program automatically updates the location information displayed in textview.

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.