Android Locationmanager provides a range of approaches to geolocation-related issues, including querying a known location, registering/unregistering periodic location updates from a locationprovider, and registering/ Unregisters a defined intent when close to a certain coordinate. Today we'll take a look at the simple use of Locatinmanager in Android to get the current location as an example.
First, we need to get an instance of Locationmanager, it is important to note that his instance can only be obtained by the following way, the direct instantiation of Locationmanager is not allowed.
Java code
- Locationmanager Locationmanager = (locationmanager) getsystemservice (Context.location_service);
After getting the instance of Locationmanager Locatonmanager, we register a periodic location update with the following statement.
Java code
- Locationmanager.requestlocationupdates (Locationmanager.gps_provider,
- 0, Locationlistener);
This code tells the system that we need to get the location information from the GPS and is updated every 1000ms, regardless of the location change. The last parameter is a reference to Locationlistener, and we have to implement this class.
Java code
- Private final Locationlistener Locationlistener = new Locationlistener () {
- public void onlocationchanged (position location) { //when coordinates change, this function is triggered, and if provider is passed in the same coordinates, it will not be triggered
- //Log It when the location changes
- if (location! = null) {
- LOG.I ("Supermap", "location Changed:lat:"
- + location.getlatitude () + "Lng:"
- + Location.getlongitude ());
- }
- }
- public void onproviderdisabled (String provider) {
- This function is triggered when the provider is disable, such as when the GPS is closed
- }
- public void onproviderenabled (String provider) {
- ///Provider triggers this function when enable, e.g. GPS is turned on
- }
- public void Onstatuschanged (String provider, int status, Bundle extras) {
- ///provider This function is triggered when a switch is available, temporarily unavailable, and no service in three states
- }
- };
These steps should generally be done in the oncreate () phase of the activity.
After successfully registering a periodic coordinate update, we can get the current coordinates at any time by the following method.
Java code
- Location location = locationmanager.getlastknownlocation (Locationmanager.gps_provider);
- Double latitude = location.getlatitude (); //Longitude
- Double longitude = location.getlongitude (); //Latitude
- Double altitude = location.getaltitude (); //Altitude
But at this time, if you try to run this locationsample, the program will probably start the error, because we do not set the GPS-related permissions, the solution is quite simple, Add the following sentence to the block in Androidmanifest.xml to resolve the permissions issue. For detailed permission settings, please refer to the official documentation docs/reference/android/manifest.permission.html
Java code
- <uses-permission android:name="Android.permission.ACCESS_FINE_LOCATION"/>
If you are debugging in the simulator, we have two methods to set up a simulated coordinate value, the first one is through DDMS, we can use this method in Eclipse's ADT plugin, just open "window"--> "Show View" opens "Emulator Control" view to see the following Settings window, we can manually, or through the KML and GPX file to set a coordinate.
Another way is to use the GEO command, we need to telnet to the 5554 port of this machine, and then enter the command line similar to the Geo fix-121.45356 46.51119 4392, the following three parameters represent the longitude, latitude and (optional) elevation.
Original Simple use of Locationmanager in Android to get the current location