Android implementation of GPS positioning code example _android

Source: Internet
Author: User
Tags stub

Obtained by GPS is a location type of longitude and latitude, can be converted to two double latitude and longitude.
Latitude: 23.223871812820435
Latitude: 113.58986039161628
First create a TextView and two button

<textview 
 android:id= "@+id/text"
 android:layout_width= fill_parent "android:layout_height=" Wrap 
  _content "  />
 
 <button
  android:id=
  @+id/btnstart" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"
  android:text= "positioning"/>
 <button android:id=
  "@+id/btnstop"
  android:layout_width= "wrap_content"
  android:layout_height= "wrap_content"
  android:text= "Stop"/ >


Then add the code for the main activity
Location is a type that holds the latitude and longitude degree
Locationmanager is the Location Management service type

Private Button btnstart;
Private Button btnstop;
Private TextView TextView;
Private Location mlocation;
Private Locationmanager Mlocationmanager; /** called the activity is a.
 * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
 
 Setcontentview (R.layout.main);
 btnstart = (Button) Findviewbyid (R.id.btnstart);
 Btnstop = (Button) Findviewbyid (r.id.btnstop);
 TextView = (TextView) Findviewbyid (R.id.text); Btnstart.setonclicklistener (Btnclicklistener); Start positioning Btnstop.setonclicklistener (Btnclicklistener);
End Position button Gpsisopen was written by himself to see if the current GPS is open getLocation is a way to get location information by itself Mlocationmanager.removeupdates () is to stop the current GPS position monitoring Public Button.onclicklistener Btnclicklistener = new Button.onclicklistener () {public void OnClick (View v) {Button b
  TN = (Button) v;
   
  if (btn.getid () = = R.id.btnstart) {if (!gpsisopen ()) return;
   
   Mlocation = GetLocation (); if (mlocation!= null) textview.settext ("Dimension:" + Mlocation.getLatitude () + "\ n Longitude:" + mlocation.getlongitude ());
  else Textview.settext ("Get No Data");
  else if (btn.getid () = = R.id.btnstop) {mlocationmanager.removeupdates (Locationlistener);
}

 }
};

 Private Boolean Gpsisopen () {Boolean bRet = true;
 Locationmanager alm = (locationmanager) this.getsystemservice (Context.location_service); if (!alm.isproviderenabled (Locationmanager.gps_provider)) {Toast.maketext (this, "GPS not turned on", Toast.length_short). Show
  ();
 BRet = false;
 else {Toast.maketext (this, "GPS is turned on", Toast.length_short). Show ();
return bRet;
 
 Determine whether the current open GPS private Boolean Gpsisopen () {Boolean bRet = true;
 Locationmanager alm = (locationmanager) this.getsystemservice (Context.location_service); if (!alm.isproviderenabled (Locationmanager.gps_provider)) {Toast.maketext (this, "GPS not turned on", Toast.length_short). Show
  ();
 BRet = false;
 else {Toast.maketext (this, "GPS is turned on", Toast.length_short). Show ();
return bRet; This method obtains the current latitude and longitude, and the first fetch is always null behind from the LocationliStener get changed position mlocationmanager.requestlocationupdates () is to open a Locationlistener wait position change private Location getLocation ()

 {//Get location Management Service Mlocationmanager = (Locationmanager) this.getsystemservice (Context.location_service);
 Find service Information the criteria criteria = new criteria (); Criteria.setaccuracy (Criteria.accuracy_fine); Positioning accuracy: Highest criteria.setaltituderequired (false); Elevation information: criteria.setbearingrequired (FALSE) is not required;  Azimuth information: criteria.setcostallowed (TRUE) is not required; Whether to allow paid criteria.setpowerrequirement (Criteria.power_low); Power consumption: low-power String Provider = Mlocationmanager.getbestprovider (criteria, true);
 
 Obtain GPS information Location Location = mlocationmanager.getlastknownlocation (provider);
 
 Mlocationmanager.requestlocationupdates (provider, Watts, 5, Locationlistener);
return location; The modified method is to wait for the GPS position to be changed to get a new latitude and longitude private final locationlistener Locationlistener = new Locationlistener () {public void onlocation Changed (Location Location) {//TODO auto-generated Method stub if (Location!= null) TextView.SetText ("Dimension:" + location.getlatitude () + "\ n Longitude:" + location.getlongitude ());
 else Textview.settext ("Get No Data" + integer.tostring (ncount)); The public void onproviderdisabled (String provider) {//TODO auto-generated a stub} public void Onproviderena Bled (string provider) {//TODO auto-generated method stub} public void Onstatuschanged (string provider, int status

 , Bundle Extras) {//TODO auto-generated Method stub}};

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.