Android obtains location data and GPS latitude and longitude via GPS _android

Source: Internet
Author: User

Copy Code code as follows:

Package com.action.android_test;
Import android.location.Location;
Import Android.location.LocationListener;
Import Android.location.LocationManager;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.TextView;

public class Mainactivity extends activity {
Private Location Location=null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get the name of the location service
String ServiceName = this. Location_service;
Get the management object for the location service
Locationmanager Locationmanager = (locationmanager) getsystemservice (serviceName);
Obtaining location data via GPS
Location = Locationmanager.getlastknownlocation (Locationmanager.gps_provider);
Updatetonewlocation (location);

Listener for/** Service management Object
Parameter 1: Positioning the way parameter 2: Listener update interval (ms) Parameter 3: Listening for updated distance (m) Parameter 4: Method of monitoring
Locationmanager.requestlocationupdates (Locationmanager.gps_provider, Watts, new Locationlistener () {

        public void onstatuschanged (String provider, int status, Bundle extras) {
       }
        public void onproviderenabled (String provider) {
        }
        public void onproviderdisabled ( String provider) {
       }
        public void onlocationchanged (Location Location) {
             Updatetonewlocation (location);
       }
   });
}
private void Updatetonewlocation (Location Location) {

TextView TV1;
TV1 = (TextView) This.findviewbyid (R.ID.TV1);
if (location!= null) {
Double latitude = location.getlatitude ();
Double longitude= location.getlongitude ();
Tv1.settext ("Latitude and longitude of: N" + "Dimension:" + latitude+ "N Longitude" + longitude);
} else {
Tv1.settext ("Inability to obtain geographic information");
}

}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

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.