Advanced Android Development (III)-exploration of positioning service (GPS) on the Android platform)

Source: Internet
Author: User

Note: Do not forget to add permissions for all of the following operations:

<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"/>
 
<Uses-permission android: name = "android. permission. INTERNET"/>
 
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
 
Sp;
LocationManager




 

You can use LocationManager to locate, track, and approaching devices. It does not need to be instantiated directly. We can use Context. getSystemService (Context. LOCATION_SERVICE). To obtain the LocationManager instance.

Common attributes and Methods

Attributes and Methods Description
GPS_PROVIDER Static String constant, indicating that LocationProvider is GPS
NETWORK_PROVIDER Static String constant, indicating that LocationProvider is a network
AddGpsStatusListener (GpsStatus. Listener listener) Add a GPS status listener
AddProximityAlert (double latitude, double longpolling, float radius, long expiration, PendingIntent intent) Add a approaching warning
GetAllProviders () Obtain the list of all locationproviders
GetBestProvider (Criteria criteria, boolean enabledOnly) Return the most suitable LocationProvider based on Criteria
GetLastKnownLocation (String provider) Obtain location information based on the Provider
GetProvider (String name) Obtain the LocationProvider with the specified name
GetProvider (boolean enableOnly) Obtain the list of available locationproviders
RemoveProximityAlert (PendingIntent intent) Delete approaching warning
RequestLocationUpdates (String provider, long minTime, float minDistance, PendingIntent intent) Periodically notifies the current Activity by the specified Provider name
RequestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) Bind the specified LocationListener listener to the specified Provider name
   

 
 
 

LocationProvider

 

 

LocationProvider is used to describe the location provider and set some attributes of the location provider. You can use the Criteria class to set conditions for the LocationProvider to obtain the appropriate LocationProvider.

Attribute or method name Description
AVAILABLE Static integer constant, indicating whether it is usable
OUT_OF_SERVICE Static integer constant, out of service
TEMPORAILY_UNAVAILABLE Static integer constant, temporarily unavailable
GetAccuarcy () Accuracy
GetName () Get name
GetPowerRequirement () Power supply requirements
HasMonetaryCost () The cost is still free.
RequiresCell () Access Base Station Network?
RequiresNetWork () Whether Intent network data is required
RequiresSatelite () Satellite access required?
SupportsAltitude () Whether high information can be provided
SupportsBearing () Provide direction information?
SupportsSpeed () Whether speed information can be provided

 

Instance: obtains all LocationProviders on the device.


LocationManager = (LocationManager) LocationManagerDemoActivity. this. getSystemService (Context. LOCATION_SERVICE );
 

 
// Obtain all locationproviders
 
List <String> allproviders = locationManager. getAllProviders ();
 
For (String string: allproviders ){
 
System. out. println (string );
 
}



 

Location class

 

 

It is used to describe the geographic location information of the current device, including latitude and longitude, direction, height and speed. You can use the LocationManager. getLastKnownLocation (String provider) method to obtain the Location instance.

Common methods and attributes:


 

Method Description
Public float getAccuracy () Accuracy
Public double getAltitude () Gain height
Public float getBearing () Direction
Public double getLatitude () Obtain longitude
Public double getlongpolling () Obtain latitude
Public float getSpeed () Acquisition speed

 


Instance: Get your location

 

// Obtain your location
 
BtnGetPosition. setOnClickListener (new View. OnClickListener (){
 
@ Override
 
Public void onClick (View v ){
 
System. out. println ("Get current location ");
 
LocationManager = (LocationManager) LocationManagerDemoActivity. this
 
. GetSystemService (Context. LOCATION_SERVICE );
 
 
 

 
 
 
Location location = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER );
 
System. out. println ("your current location:"); www.2cto.com
 
StringBuilder sb = new StringBuilder ("your current location (longitude and latitude ):");
 
Sb. append ("(");
 
Sb. append (location. getLatitude ());
 
Sb. append (",");
 
Sb. append (location. getlongpolling ());
 
Sb. append (")");
 
System. out. println (location. getLatitude ());
 
System. out. println (location. getlongpolling ());
 
TxtPostion. setText (sb );
 
}
 
});
// Obtain your location

BtnGetPosition. setOnClickListener (new View. OnClickListener (){

@ Override

Public void onClick (View v ){

System. out. println ("Get current location ");

LocationManager = (LocationManager) LocationManagerDemoActivity. this

. GetSystemService (Context. LOCATION_SERVICE );

 

 

Location location = locationManager. getLastKnownLocation (LocationManager. GPS_PROVIDER );

System. out. println ("your current location :");

StringBuilder sb = new StringBuilder ("your current location (longitude and latitude ):");

Sb. append ("(");

Sb. append (location. getLatitude ());

Sb. append (",");

Sb. append (location. getlongpolling ());

Sb. append (")");

System. out. println (location. getLatitude ());

System. out. println (location. getlongpolling ());

TxtPostion. setText (sb );

}

});

 
 

Instance: Tracking Location:

RequestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

About the minTime parameter, the minDistance parameter indicates how long the user's location will be updated when the user's location changes.


LocationManager. requestLocationUpdates (
 
LocationManager. GPS_PROVIDER, 500,500, new listener ());
LocationManager. requestLocationUpdates (

LocationManager. GPS_PROVIDER, 500,500, new listener ());

 
Instance: Get the best LocationProivider


LocationManager. getBasetProvider (Criteria criteria, boolean is)
LocationManager. getBasetProvider (Criteria criteria, boolean is)

 
Criteria class

 
It encapsulates the conditions used to obtain the LocationProvider. You can filter and obtain the LocationProvider according to the specified Criteria conditions.
And common attributes and methods are as follows:

Attribute or access name Description
ACCERACY_COARSE Rough precision
ACCURACY_FINE High Accuracy
POWER_HING High power usage
POWER_LOW Low Power Consumption
IsAlititudeRequried () Returns whether the Provider requires height information.
IsBearingRequired () Returns whether the Provider requires location information.
IsSpeedRequried () Returns whether the Provider requires speed information.
IsCostAllowed () Allow fees
SetAccuracy (int accuracy) Set Provider precision
SetAltitudeRequired (boolean altitudeRequired) Sets whether the Provider requires height information.
SetBearingRequired (boolean bearingRequired) Sets whether the Provider requires orientation information.
SetCostAllowed (boolean costAllowed) Set whether the Provider generates fees
SetSpeedAccuracy (int accuracy) Set whether the Provider requires speed information
GetAccuracy () Accuracy

 
 



 

// Obtain the best Provider
 
BtnBestProvider. setOnClickListener (new View. OnClickListener (){
 
@ Override
 
Public void onClick (View v ){
 
String bestProviders = "";
 
LocationManager = (LocationManager) LocationManagerDemoActivity. this
 
. GetSystemService (Context. LOCATION_SERVICE );
 
// Create a new Criteria
 
Criteria criteria = new Criteria ();
 
// Set the accuracy
 
Criteria. setAccuracy (Criteria. ACCURACY_COARSE );
 
Criteria. setPowerRequirement (Criteria. POWER_LOW );
 
Criteria. setAltitudeRequired (false );
 
Criteria. setBearingRequired (false );
 
Criteria. setSpeedRequired (false );
 
Criteria. setCostAllowed (false );
 
// Obtain the qualified provider
 
BestProviders = locationManager
 
. GetBestProvider (criteria, false );
 
TxtCrerita. setText ("best provider:" + bestProviders );
 

 
}
 
});
// Obtain the best Provider

BtnBestProvider. setOnClickListener (new View. OnClickListener (){

@ Override

Public void onClick (View v ){

String bestProviders = "";

LocationManager = (LocationManager) LocationManagerDemoActivity. this

. GetSystemService (Context. LOCATION_SERVICE );

// Create a new Criteria

Criteria criteria = new Criteria ();

// Set the accuracy

Criteria. setAccuracy (Criteria. ACCURACY_COARSE );

Criteria. setPowerRequirement (Criteria. POWER_LOW );

Criteria. setAltitudeRequired (false );

Criteria. setBearingRequired (false );

Criteria. setSpeedRequired (false );

Criteria. setCostAllowed (false );

// Obtain the qualified provider

BestProviders = locationManager

. GetBestProvider (criteria, false );

TxtCrerita. setText ("best provider:" + bestProviders );

}

}); Www.2cto.com

 
Conclusion (see GPS again ):

From the above knowledge, we can know that we need to use the LocationManager System Service to use the GPS service on the Android platform. We can use the Context. getSystemService (Context. LOCATION_SERVIER) to get the LocationManager object instance. With such a service, we can use LocationProvider to describe the location provider, for some reason, we have used another Criteria class to set our own best requirements. When everything is done, we use the Location class to get our own Location, this class is used to obtain our own location, such as longitude and latitude.

 

From jiahui524 Column

 

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.