LBS (Location Based Services) is based on geographical service, there are at least two layers of meaning, the first to be able to easily obtain the current geographical location, such as latitude and longitude elevation, and the other is based on the current position to provide value-added services, such as to find nearby gas stations, Restaurants, hotels and so on. This is the first step: Get the user's current location, we can use the Android GPS service to get. Android offers both web-based positioning services and satellite-based location-based services. The top three in setting the-> position and security settings is that the last enhanced GPS is designed to help quickly find satellites.
So let's write a simple program now to get the functionality of the current location.
1. New Project Lesson26_gpslocation
2,Mainactivity.java code is as follows:
Package basic.android.lesson26;
Import android.app.Activity;
Import Android.content.Context;
Import Android.location.Criteria;
Import android.location.Location;
Import Android.location.LocationListener;
Import Android.location.LocationManager;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;
public class Mainactivity extends activity {TextView TV1;
Location Location;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Defines the UI component Button B1 = (Button) Findviewbyid (R.id.button1);
TV1 = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Gets the Locationmanager object Locationmanager lm = (locationmanager) this.getsystemservice (Context.location_service);
Define the criteria criteria = new criteria (); Set positioning accuracy Criteria.accuracy_coarse relatively coarseSlightly, the criteria.accuracy_fine is comparatively fine criteria.setaccuracy (criteria.accuracy_fine);
Sets whether elevation information is required altitude criteria.setaltituderequired (true);
Set whether azimuth information is required bearing criteria.setbearingrequired (TRUE);
Set whether operators are allowed to charge criteria.setcostallowed (true);
Set the demand for power supply criteria.setpowerrequirement (Criteria.power_low);
Get GPS information provider String Bestprovider = Lm.getbestprovider (criteria, true);
LOG.I ("Yao", "Bestprovider =" + Bestprovider);
Get positioning Information location = Lm.getlastknownlocation (Bestprovider); Bind the button to a point click Listener B1.setonclicklistener (New View.onclicklistener () {@Override public void OnC
Lick (View v) {updatelocation (location);
}
}); Position listener Locationlistener Locationlistener = new Locationlistener () {//@Overri triggered when position changes De public void onlocationchanged (Location locatiON) {log.i ("Yao", location.tostring ());
Updatelocation (location);
The @Override public void onproviderdisabled (String arg0) {is triggered when provider expires) {
LOG.I ("Yao", arg0);
@Override public void onproviderenabled (String arg0) {provider when available
LOG.I ("Yao", arg0); @Override public void onstatuschanged (String arg0, int arg1, Bun, provider state change)
Dle arg2) {log.i ("Yao", "onstatuschanged");
}
};
500 ms Update once, ignoring position changes lm.requestlocationupdates (bestprovider, 0, Locationlistener); }//update location information private void Updatelocation (Location Location) {if (Location!= null) {Tv1.sett EXT ("Locate object information as follows:" + location.tostring () + "\n\t Longitude:" + location.getlongitude () + "\n\t its latitude:" + location . GetlatiTude ());
else {log.i ("Yao", "not getting the anchored object location"); }
}
}
3, Main.xml I do not paste code, just a button and a text box, wait a moment to see the screenshot will understand.
4, to add permissions in the Androidmanifest.xml:
<!--rough location authorization-->
<uses-permission= "" Android:name= "Android.permission.ACCESS_COARSE_LOCATION" >< /uses>
<!--fine positioning authorization-->
<uses-permission= "Android:name=" android.permission.ACCESS_FINE_ LOCATION "></uses>
<!--analog positioning authorization-->
<uses-permission=" "Android:name=" Android.permission.ACCESS_MOCK_LOCATION "></uses>
<!--network access authorization-->
<uses-permission=" " Android:name= "Android.permission.INTERNET" ></uses>
5. Compile and run the program on the real machine to see the result:
Here to explain, I submit the simulator data on the simulator is not successful, the test on the real machine is not very smooth, a hardware-related will always have some inexplicable mistakes ...
The above is the Android GPS introduction and simple example, hope to help the students in need, thank you for your support to this site!