Mobile phone programming, calling GPS positioning module

Mobile phone programming, calling GPS positioning module

GPS calling is a very important function in the Android system, which can provide all functions related to location for the mobile phone app.

When programming Android, there is a special management class for the GPS module, called: LocationManager, used to manage all interfaces related to GPS positioning services.

LocationMangager

Referred to as the location manager. Before using GPS related hardware devices, you need to set a LocationManager. Object acquisition of the LocationManager class is not directly created, but is provided by the system. You only need to create a reference to the LocationManager object and assign the data provided by the system to it:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

This class encapsulates some commonly used GPS functions, such as obtaining GPS status, recent location information, etc .:

GpsStatus.Listener, GPS status monitoring, including GPS start, stop, first positioning, satellite change and other events.

GpsStatus, GPS status information, we used GpsStatus when the satellite status changed.

GpsSatellite, positioning satellite, contains information such as the satellite's position, altitude, pseudo-random noise code, and signal-to-noise ratio.

GpsStatus getGpsStatus (GpsStatus): Get the current GPS status;

Location getLastKnownLocation (String): Get the last available location information

You can pass location information to a Location object:

Locationlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

Location

For location information, you can obtain location information such as time, latitude, longitude, and altitude through Location. The above uses onLocationChanged () in locationListener to get the location,

For example, get time location.getTime (), get longitude getLongtitude (), get latitude: location.getLatitude (), altitude location.getAltitude (), etc.

The following describes how to actively obtain the location. First, create a reference to the Location object and assign it:

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

And show it:

system.out.println ("Time:" + location.getTime ());

system.out.println(“经度:”+location.getLongitude());

In addition, if the value is assigned by new Location, then the value of all functions will be 0 at this time. The correct data could not be obtained.

Location location=new Location(LocationManager.GPS_PROVIDER)

You can call the following functions to perform the operations we want for each updated location information:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 10, new LocationListener())

The first parameter is the LocationProvider object, the second parameter is the refresh time difference, set here to 1 second, the third parameter is the location difference, set here to 10 meters, and the fourth parameter is a location listener object.

Simple Coding + Annotation

!-Declares mobile phone coarse positioning authorization –
uses-permission android: name = “android.permission.ACCESS_COARSE_LOCATION” />
!-Declares mobile phone fine positioning authorization->
uses-permission android: name = “android.permission.ACCESS_FINE_LOCATION” />
!-Declare mobile phone simulation positioning authorization->
uses-permission android: name = “android.permission.ACCESS_MOCK_LOCATION” />
!-Declare networking authorization->
uses-permission android: name = “android.permission. INTERNET "/>

Published 9 original articles · liked 0 · visits 924

Guess you like

Origin blog.csdn.net/weixin_46146588/article/details/105451900