
What is Location Listener in Android ?
Location Listener is Used to receiving notifications from the LocationManager when the location has changed. These methods are called is LocationListener has been registered with the location manager service.
Location class is geographic location. It contains latitude and longitude etc.
Location Manager is provide the access system is Location Service.
Location object represents a geographic location it can consist a latitude, longitude, time stamp, and other information such as bearing, altitude and velocity.
In this Tutorial to Create Get the current location using location listener.And Get the current latitude and longitude in the location listener.How to get the current location in android to implements Location Listener and some Methods are implements and then get the location without Google API used.
In this Example i have not Google API used.I have a used to Location listener in android built in package and get the current location.
Methods of Location Object Class
distanceTo(Location location) : It is used to distance in meters between this location.
getAccuracy() : It is used to Get the estimated accuracy of this location, in meters.
getAltitude() : In this Method is used to Get the altitude if available, in meters above sea level.
getLatitude() : In this method is used to Get the latitude, in degrees.
getLongitude() : In this method is used to Get the longitude, in degrees.
getBearing() : In this Method is used to Get the bearing, in degrees.
getSpeed() : In this method is used to Get the speed if it is available, in meters/second over ground.
setLatitude(double latitude) : In this method is used to Set the latitude, in degrees.
setLongitude(double longitude) : In this Method is used to Set the longitude, in degrees.
Implements Methods of Location Listener
onLocationChanged(Location location) : In this Method is Called when the location has been changed.
onProviderDisabled(String provider) : In this Method is Called when the provider is disabled by the user.
onProviderEnabled(String provider) : In this Method is Called when the provider is enabled by the user.
onStatusChanged(String provider,int status,Bundle bundle) : In this method is Called when the provider status changes.
Implements LocationManager Type
LocationManager.GPS_PROVIDER : In this Type used to location using satellites. this provider return a location fix.
LocationManager.NETWORK_PROVIDER : In this Type used to location based on the availability of nearby cell towers and WiFi access points. This is faster than GPS_PROVIDER.
It is used to implements methods and Types of Location and you can used to different different methods and type implements to get the current location.
How to Get Current Location Latitude And Longitude Example
Declared Permission in AndroidManifest.xml File.
1 2 3 |
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> |
Create activity_main.xml File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bhaumik.currentlatlong.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:layout_marginLeft="31dp" android:layout_marginStart="31dp" android:layout_marginTop="49dp" android:text="Latitude" android:textSize="22dp" android:textStyle="bold" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" android:layout_below="@+id/textView" android:layout_marginTop="35dp" android:text="Longitude" android:textSize="22dp" android:textStyle="bold" /> <TextView android:id="@+id/tv_lat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/textView2" android:layout_marginLeft="86dp" android:layout_marginStart="86dp" android:layout_toEndOf="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:textSize="19dp" /> <TextView android:id="@+id/tv_long" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView2" android:layout_marginLeft="58dp" android:layout_marginStart="58dp" android:layout_toEndOf="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:textSize="19dp" /> </RelativeLayout> |
Create MainActivity.java File Follow this Code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
package com.bhaumik.currentlatlong; import android.Manifest; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Build; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements LocationListener{ TextView tv_lat,tv_long; LocationManager locationManager; public static final int PERMISSION_CODE = 100; LocationListener locationListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_lat = findViewById(R.id.tv_lat); tv_long = findViewById(R.id.tv_long); if (!checkPermission()){ requestPermission(); } locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,50,1,this); } public boolean checkPermission(){ int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION); int result1 = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION); return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED; } public void requestPermission(){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION },PERMISSION_CODE); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case PERMISSION_CODE : if (grantResults.length > 0){ boolean location = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean location1 = grantResults[1] == PackageManager.PERMISSION_GRANTED; if (location){ Toast.makeText(this, "Permission Granted..", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "Permission Denied..", Toast.LENGTH_SHORT).show(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ showMsg("You need to allow access to the Permission", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_CODE); } } }); return; } } } } } } private void showMsg(String s, DialogInterface.OnClickListener onClickListener) { new AlertDialog.Builder(MainActivity.this) .setMessage(s) .setPositiveButton("OK",onClickListener) .setNegativeButton("Cancel",null) .create() .show(); } @Override public void onLocationChanged(Location location) { tv_lat.setText("" + location.getLatitude()); tv_long.setText("" + location.getLongitude()); Log.d("tag","Latitude -> " + location.getLatitude()); Log.d("tag","Longitude -> " + location.getLongitude()); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } |

Above the Example of Create Simple Get Current Location using Location Listener. In this example to declared some permission and then check the runtime permission in location. you can display the permission allow or deny and then fetch the current location in your device and then display the textView in latitude and longitude.