If your phone/device we use has GPS sensors, we can use it to check our position programmatically. We can also use the free OpenStreetMap project ( http://openstreetmap.org ) and create your own navigation.

We will start by creating a new project and editing the AndroidManifest.xml file for this project. We need to add a request for permission to use GPS sensors. Without such authorization, our program will not work. Add lines:

<uses-permission android: name = “android.permission.ACCESS_COARSE_LOCATION” />

<uses-permission android: name = “android.permission.ACCESS_FINE_LOCATION” />

Just under the closure of the element. xyzzz:

 

We already have the ability to use GPS sensors. Time to go to the right version of the code. We will always start with the simplest version, ie when the program is started, you will see our location on the screen, ie latitude and longitude.

We are now moving to our main activity. I am sticking three elements of the TextView class on the screen. The length and latitude, and the name of the selected provider (this will be explained later).

 

Immediately in the method onCreate hooks to them handles. I will appeal to these elements, so I will also need elements that represent these components.

We will use the LocationManager object to determine the location. I define it as a TextView component as a class field and not as a private variable in the onCreate method. I’m doing this because maybe I will be referring to this object from other methods at the later stages of program creation.

 

Criteria (line 29) is used to search for the best GPS location provider. For example, I can specify that I am interested only in free vendors (the setCostAllowed class Criteria) or the power demand level for a given setPowerRequirement. In order to apply one of these conditions, after creating a Criteria object, we run the selected method for this object. The Criteria object is used as an element of selection for the supplier when we retrieve the name of the best supplier. On line 30, I initialize an object using the service returned by the getSystemService method (inherited from the Activity class). Using this method we can hook up to the most diverse system services: use different sensors (temperature, pressure etc), use location services, download data from the internet, print, use USB port and much more. Since we will use location services, I will provide LOCATION_SERVICE as the getSystemService parameter. Line 31 is the name of the best location provider (for example, GPS, triangulation, or WIFI). Only the provider for whom the activity has permission is allowed (the entries are in AndroidManifest.xml). If several suppliers meet the criteria, then the name of the highest accuracy will be returned. For example, if we can determine our position based on GPS, or based on triangulation, With GPS set to within 10 meters and triangulation to within 1 kilometer, the name of the gps provider will be returned. The first parameter of the getBestProvider method is our criteria, the second determines whether only active suppliers are to be returned (in this case we are of course only interested).

 

The Location class will represent our location in space, we will later retrieve it for example, our latitude and longitude. First, however, we need to specify our location 🙂 We do this in line 32 using the getLastKnownLocation method of the LocationManager object. As a parameter we give the name of the selected location information provider. Lines 34-36 is simply a display of the obtained data. Note the getLongitude and getLatitude methods that return the length and latitude. Returns the number of double, with quite high accuracy. Being on the outskirts of Warsaw, I got my position with the accuracy of 7 decimal places. For this simple program, it would be worthwhile to get a handle on a situation in which there would be no supplier, or you could not determine the location. We will not do this here, because the return nulla method (here in getBestProvider and getLastKnownLocation) is the basis of Java. To test things with GPS, it is best to do it on a real device and not an any emulator. It is possible to set the GPS location on emulators, but this does not always work. When I started the program on my phone it showed:

best provider: network

latitude: 52.2996591

longitude: 20.9930961

The program works, however, if we change the location, our program will not record it. It would be worthwhile to enrich the program with functionality that would take into account changes in our location. I will start by adding an item to the screen where the location history will appear. I’ve pasted a component of the SmallText class.

 

To make history a little different, I changed the text in this new component by editing the layout file and added the lines: android: textColor = “# 300FD”

 

We will look at the source code of our activity from the top. In the definition, I added “implements LocationListener”, so I will have an available method called onLocationChanged which is invoked every time a new location is detected. Since this is an interface, I will have to implement the four methods I need. We’ll take care of this in a moment.

 

In line 18 we see a new field – t4. This is an object representing a component for displaying history. In lines 25-28 we see a new method of “refresh”. Since I will have to refresh my position at least twice, ie when I start the application and when I change the location, I decided to code the postage to be posted to a separate method.

On line 37 we see a link to the t4 object reference to our new component (the one that will do the history). Nothing extraordinary, but we must remember that. On line 41 instead of calling separately the best search provider and download location we have a call to the method that just does this and updates our objects. Line 42 includes the property of refreshing. We are configuring here at what time and at what location the system is supposed to refresh the location (ie, how frequently the onLocationChanged method is to be automatically invoked – in lines 50-57). The first parameter is the name of the provider we use, the second is the time in milliseconds what is the refresh rate, third is what distance (in meters) The fourth is the object that implements the LocationListener interface (this is where the object is located). Lines 43-46 is the default setting, which is what happens after the program starts. Here we show the provider and coordinates as before. On line 46 I set the first and so far only history line.

 

Below are four methods that I had to implement because of the implementation of the LocationListener interface. Three temporarily leave alone. I’m only interested in the onLocationChanged method. It is called a time interval or a specified distance (line 42) in the requestLocationUpdates method. As part of my implementation, the onLocationChanged method refreshes the location by calling the refresh method and then displays the current location on lines 52-54. In line 55, I add another line to the history.

I was curious how well it worked, so I downloaded the program on the phone and went to check if there was a corner around me. Here are the results: