Determine Current Location Using the Location Manager in Android

Introduction

 
This article explains how to determine the current location using the Location Manager in Android. Android Studio is used to develop the application.
 
This application displays the latitude and longitude by getting information from the system.
 
First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to the getLastKnownLocation() as an argument to get the location. Finally, get the latitude and longitude by calling the getLatitude() and getLongitude() methods.
 
Step 1
 
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:id="@+id/textview1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="@string/hello_world" />  
  16.     <TextView  
  17.             android:id="@+id/textview2"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:layout_marginTop="30dp" />  
  21.     <TextView  
  22.             android:id="@+id/textview3"  
  23.             android:layout_width="wrap_content"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_marginTop="60dp" />  
  26. </RelativeLayout> 
Step 2
 
First, create a Location Manager object by calling the getSystemService() method and LOCATION_SERVICE as an argument. Call getBestProvider() to get the location in the form of a string. Now the provider to getLastKnownLocation() as an argument to get the location. Finally, get the latitude and longitude by calling the getLatitude() and getLongitude() methods.
 
Create a Java file and write this:
  1. package com.currentlocation;  
  2. import android.content.Context;  
  3. import android.location.Criteria;  
  4. import android.location.Location;  
  5. import android.location.LocationListener;  
  6. import android.location.LocationManager;  
  7. import android.os.Bundle;  
  8. import android.app.Activity;  
  9. import android.view.Menu;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.    
  13. public class MainActivity extends Activity implements LocationListener {  
  14.    
  15.    LocationManager locationmanager;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.    
  21.    locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);  
  22.    Criteria cri=new Criteria();  
  23.         String provider=locationmanager.getBestProvider(cri,false);  
  24.    
  25.         if(provider!=null & !provider.equals(""))  
  26.         {  
  27.             Location location=locationmanager.getLastKnownLocation(provider);  
  28.             locationmanager.requestLocationUpdates(provider,2000,1,this);  
  29.        if(location!=null)  
  30.        {  
  31.            onLocationChanged(location);  
  32.        }  
  33.        else{  
  34.            Toast.makeText(getApplicationContext(),"location not found",Toast.LENGTH_LONG ).show();  
  35.        }  
  36.         }  
  37.         else  
  38.         {  
  39.         Toast.makeText(getApplicationContext(),"Provider is null",Toast.LENGTH_LONG).show();  
  40. }  
  41.     }  
  42.     @Override  
  43.     public boolean onCreateOptionsMenu(Menu menu) {  
  44.         // Inflate the menu; this adds items to the action bar if it is present.  
  45.         getMenuInflater().inflate(R.menu.main, menu);  
  46.         return true;  
  47.     }  
  48.     @Override  
  49.     public void onLocationChanged(Location location) {  
  50.         TextView textView2=(TextView)findViewById(R.id.textview2);  
  51.    
  52.         TextView textView3=(TextView)findViewById(R.id.textview3);  
  53.    
  54.         textView2.setText("Latitude"+location.getLatitude());  
  55.         textView3.setText("Longitude"+ location.getLongitude());  
  56.     }  
  57.     @Override  
  58.     public void onStatusChanged(String s, int i, Bundle bundle) {  
  59.     }  
  60.     @Override  
  61.     public void onProviderEnabled(String s) {  
  62.     }  
  63.     @Override  
  64.     public void onProviderDisabled(String s) {  
  65.     }  
Step 3
 
Perform the following changes in the Android Manifest.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.currentlocation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  7.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>  
  8.     <uses-sdk  
  9.         android:minSdkVersion="7"  
  10.         android:targetSdkVersion="16" />  
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.currentlocation.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. </manifest> 
Step 4
 
Set latitude and longitude in Android Studio DDMS to run the application on the emulator as in the following:
 
Clipboard02.jpg
 
Step 5
 
Clipboard04.jpg


Similar Articles