Description: This article is related to spytec login. The writer mainly has a tutorial on making a GPS location tracker app for Android. We will use location listener to handle location changes and use Google maps to display marker in maps.
I’m refresher. If that’s difficult to pronounce for you, you can call me Wish. This is the first content from the code city. It might not be good. Let’s face it, you will learn something. If you still have a question, you can drop them in the comment section below. I will answer all of your questions.
Let’s begin, we will install an Android app that tracks your location. Wherever you go, the master in the map will follow. Open up your Android studio, go to the file, select new project and write any name. I choose location tracker and click on next. From this screen, you can select add new activity. Then you’ll have to create your own layout and your own activity class. Let’s select Google Maps activity, click Next and click next on the following screen. Then click finish.
You have finished creating the project, you will see something like Google Maps, API dot, XML. You need to enter your API key, then the app is ready to be installed. You can try it out on your phone, you’ll see a map and the marker in Australia. To track your location, we need to do something more.
So let’s get your API key, Let’s go to developers.Google.com/map/documentation/ Android API. The link is in the description below. Click on this and get a key button on the top right. Type the name of your app, you can type any name. It doesn’t have to be the name that you selected in a studio project. Click on create and enable API. You’ll see your API key.
You need to use this key and get your project. So copy this key and place it in this highlight section. Go back to your Android studio and place it here. Now you get everything ready. It is a simple app with marker. You can try and run it on the phone. Now you can pause the content and try it.
But you will not be able to do this because you don’t have the necessary permission. In Android, you need permission from user. So this requires access to Internet and access to location information. You need to specify it in the manifest, you can specify it using the following permission. It must be in the screen. Or if you have a mobile phone, you can check out the permission in the description.
After you enter your API key and the permission in manifest, you can build a pilot, you’ll see a map and a marker in Australia. Now we need to get our location and show it on a map. Open this activity on a SCORM app from store XML. You will see something like this, the screen menu displays the map. There are some descriptive comments by Google, you don’t need that, let’s remove it. I don’t like so many comments.
Let me explain what this code is. This first line is simple. Set content view is setting the layout of the view. We are fetching our map fragment from our layout and we are using this asynchronous function. This function gets the Google Maps from background. The parameter is a Google map, this Lat Lang is like a latitude and longitude. Then we can add a marker using this map. I think that this 34,151 is a new coordinates of Sydney because it’s predefined in this project.
It is moving the camera to a particular geographic coordinates in this scenario. It is moving to this latitude line. We created Sydney, it is moving the camera to Sydney’s location. You might notice that I’ve added this location listener interface. so what’s happening is whenever your location changes, the location manager will call a method on location change. So you need to implement this interface.
The red line means an error because it’s the Mexican. Click on enter and implement all the methods, it pops up. You’ll see these methods on location change. The on state has changed, we will be able to use this on location change. We need to update the perspiration whenever the location changes.
So create these new variables on the top, we call it coordinates, the location manager calls it location manager. Now we will create a new method, we’ll call it request location. We are using a criteria class and the location manager to get provider. Then using that provider, we request location things. So what is a criteria? A criteria helps you get your location data according to a preferred accuracy and power requirement.
The accuracy is decided by the power. Copy the code and get the location data using location manager in Android. We pass the parameters providers, this 10,000 is the duration of update in in which we can get the location date. So how long do we need to check the location? It’s in minutes and milliseconds. 10,000 milliseconds is 10 seconds. So after 10 seconds, it requests the user’s location again. 10 meters is the distance that must be covered for a new location.
We need to write some codes, they are nothing important, they are the most of this variables, you don’t need that. If you want to ban it, you will use this on a device that is running lower than marshmallow. But do you like this? My bonus is important, you might have to learn it.
Now these permissions all go to one, it is request code. So it will not be able to recognize the reply. Is it Carrie? Is it repaired to our request or is it the reply to some other requests? The got permission dot access course location and promotion dot access file location are required for getting location information. These marker options and marker are new, we’ll talk about later.
In this oncreate function, we have initialized location manager. Then we check and initialize marker options. We set the land latitude and longitude of our marker as zero zero. There is somewhere around equator. So excuse me before we get our actual location. The marker will be over zero comma zero. Then we check if our belt version is over.
If it is over, we request permissions or request location directly. This final condition is to check if the device is allowed to give location information to apps. If the user has allowed location access to apps, it will check if the location services are enabled. If they are not enabled, we will be shown an alert. We’ll show you what this alert is.
Since we had requested permission location information in this, your system will return some locations on machine. We need to handle that information, we show a marker over that latitude and longitude. In this on location change method, I am creating a new Latitude Lang variable and my coordinates.
Then I initialize it with latitude and longitude of locations or longitude. Then I set the markers position to the currently fresh coordinates, I move our map apps camera to that location. So I’m moving on maps camera to our new location. We have done it.
If you run on the version of marshmallow, you will not need to do anything. We only need to request permissions that we have not created. So here’s the code for its location to enable and use permission granted. I think that you understand the differences between permissions granted and location enabled.
Let me tell you again, this location is enabled to check if apps in our device are allowed to access location information. The permissions are granted to check if our specific apps have the required permissions to access location information.
If you understand, you can message me. I’ll be happy to help you. If you have any doubt, you can message me. The full source code is available in the description below. Thank you. See you next time.