Get location information

Source: Internet
Author: User

Once the Locationmanager object is obtained, the location information can be obtained by specifying Locationprovider.

The following procedure shows how to get location information in real time via mobile phone, including the latitude, longitude, height, direction, movement speed, etc. of the user's location.

Import android.location.Location;
Import Android.location.LocationListener;
Import Android.location.LocationManager;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import Android.view.Menu;
Import Android.widget.EditText;

public class Locationtest extends Activity {
Defining Locationmanager Objects
Locationmanager Locmanager;
Defining the EditText component in the program interface
EditText Show;

@Override
protected void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_location_test);
//Get the EditText component on the program interface
show = (EditText) Findviewbyid (r.id.show);
//Create Locationmanager Object
Locmanager = (locationmanager) getsystemservice (context.location_service);
//Get the nearest location information from GPs
Location Location = locmanager.getlastknownlocation (locationmanager.gps_provider);
//Use location according to EditText's display
Updateview (location);
//Set GPS location information once every 3 seconds
locmanager.requestlocationupdates (Locationmanager.gps_provider,
8, New Locationlistener () {

@Override
Public void Onstatuschanged (String provider, int status, Bundle extras) {
}

@Override
Public void onproviderenabled (String provider) {
//When GPS Locationprovider is available, update location
Updateview (locmanager.getlastknownlocation (provider));
}

@Override
Public void onproviderdisabled (String provider) {
Updateview (null);
}

@Override
Public void onlocationchanged (location location) {
//Update location When GPS location information is changed
Updateview (location);
}
});
}
//Update the content shown in EditText
Public void Updateview (location newlocation) {
if (newlocation! = null) {
StringBuilder sb = new StringBuilder ();      
sb.append ("Real-time location information: \ n");
sb.append ("Longitude:");
Sb.append (Newlocation.getlongitude ());
sb.append ("\ n Latitude:");
Sb.append (Newlocation.getlatitude ());
sb.append ("\ n height:");
Sb.append (Newlocation.getaltitude ());
sb.append ("\ n speed:");
Sb.append (Newlocation.getspeed ());
sb.append ("\ n Direction:");
Sb.append (newlocation.getbearing ());
Show.settext (sb.tostring ());
}else{
//If the incoming location object is empty, empty the EditText
Show.settext ("");
}
}
}

In the program above, the bold code is used to obtain location information from the site, including information such as the user's latitude, longitude, height, direction, and movement speed. In the program code, a listener is also set up through Locationmanager, which is responsible for each 3-second request for location information to the Locationprovider, when the Locationprovider is available, when it is unavailable, or when the location information provided is changed, The system will callback Updateview (location newlocation) to update the positioning information displayed in the EditText.

The program requires access to the GPS signal, so you need to add the following authorization code snippet in the Androidmanifest.xml file:

<!--authorization to get location information--
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>

If the program is combined with Google Map, the program can develop a GPS navigation system based on the information provided by the GPS in real time to show the location of the user on the map.

Get location information

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.