Location Listener

Dhoyazan Adbo Othman
A free video tutorial from Dhoyazan Adbo Othman
Senior Trainer and Developer at Pioneers Academy
4.2 instructor rating • 9 courses • 29,619 students

Learn more from the full course

Android Cars App using Kotlin, PHP and Google Maps

Kotlin, Volley, PHP, SMS and Google Map API with features like Uber taxi app

08:07:23 of on-demand video • Updated May 2018

  • Understand Kotlin Programming Language from Scratch
  • Implement OOP concepts in Kotlin
  • Create PHP web service connected to MySQL database
  • Create android app using Kotlin
  • Use Volley and Picasso libraries to get data from PHP web services
  • Use Google Map API
  • Send SMS
  • Add Run-time permissions
  • Create car service project for customer and employee
English [Auto] How are everyone in these. We are going to talk about the location literally the location listener is a class used for receiving notifications from the location manager which has been discussed in the previous video with a location change. So with the user change its location automatically the location listener gets notified that the user location has been changed and this location listener will store the Longet youth and the latitude of the new location for that user. This location listener has four function. The first one is on location change and this function is executed by default when the user changes location. Here when the user enable or disable his provider which means if the user switch on the service of the G B S or switch of the service. And here with the status of the GP s is changed from on to off and vice versa. Now how can we connect the location listener with the location manager to connect your location manager with the location listener you have a function call request location or does this function is used to connect the location manager with the location listener and this function has four parameters. Number one. What is the location provider. You can use either G-B s or network. Then when should the location which is the language used and latitude changed. Which means if you need it according to your nation you can set something like. Five seconds each. And second I need to get updated for this location of course also you can get updated. According to the distance which means each 100 meter 200 meters and so on. And the last one is the name of the location. Of course if you dont need to use the duration and distance but zero for both duration and distance. Let's try now to make example which get the location of the User Device this case lets back to our application and this is our first example lets remove this one. Of course we don't need Also this code. And no we need to fix it. This is the first Tick's view to get the longest would and I need another takes a view to get the latitude your the other takes if you for the lottery and I'm going to call this a view as TV underscore or yes. And the other one TV underscore luck which means longitude and latitude. Now how can I get the longest used and latitude with the user device has changed. At first you should define the location manager object as we have discussed in the previous video. Then we should define another object. Called that listener for example. And it is a class called location listener. And then use this Cutlip bracket. This object is used to call the overridden method which is for method which has been discussed in the Park Point slide on location change on provider enable and so on. So just boot the pointer here and end by keyboard use alt enter or of course instead of in turn you can use this simple to select implement member then select all those functions and click on it. In this case you have called the four function which is a location change then or states change on provider enable. And of course on provider disabled. So as you can see here we have those four function. I'm going to use this one which is on location change this function is executed by default with the user defined location has been changed. Now it will store the new location here in this variable which is zero. Let's remove this question mark just to not allow the null value and I'm going to write something like Villon that text equal zero dot longitude dot to a string which means get. Then you'll get used and show it inside this text. And because the Long truth is of data type double I have custody two string also according to the latitude equal zero dot lotted you got two strings. So in this case you have printed the longest you had on the first X if you and the latitude on the second takes. Then the last thing how can we connect our manager with the listener. This case I'm going G.R. before the last two Carol-Lee bracket and I'm going to use this function Munsch or dot request location objects. But before you use this function you have to define the required permission for this example to get the location of the user. You should use two permission. So let's go here to the manifest in this file which is droite manifest and here before the application type. You can use something like. Uses permission which is called Access course location. And another one uses Burma-Shave access find location. So to get the location of the user you should use those two permission which is the access course location and the access file location. Now when we go again to our Codlin code at first the first one defines the provider this example I am going to define the gigas provider. Now I don't need to add the Times and also I don't need to add the duration and the last thing will beat the listener OK. So those are the four barometers. Those are the four parameter which are the jenius provider zero here. There's also zero here. And these are of course don't but mean time and mean distance because they are by default. Now with the pointer here and again select this one and add here at supper is missing. So now everything is ready. The example is ready to get the location of the user. So let's run this example now. And let's click on OK. Now how can we send Longet you and latitude here from the simulator just click here. And you can send any number you need here from Longet youth or latitude from the location section. Let's suppose we are going to send something like anything for example here. Point and the number and click on send. Yes. As you can see here we have got the location of the user. Of course when you test this example on physical Mumbai you don't need to send any location because the mobile will get your location by default. So in this video we have talked about how can we get the location of the mobile phone by using the class location. See you in the next video.