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

Source: Internet
Author: User

 

Exploration of positioning service (GPS) on Android platform)

 

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" /> 

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, 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: Get all locationproviders on the device

Locationmanager = (locationmanager) locationmanagerdemoactivity. this. getsystemservice (context. location_service); // obtain all locationprovider lists <string> allproviders = locationmanager. getallproviders (); For (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 position 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 = 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());

Instance: Get the best locationproivider

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 = new criteria (); // set the accuracy of 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 );}});

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.

 

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.