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. <TextView
  11. android:id="@+id/textview1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world" />
  15. <TextView
  16. android:id="@+id/textview2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="30dp" />
  20. <TextView
  21. android:id="@+id/textview3"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginTop="60dp" />
  25. </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. public class MainActivity extends Activity implements LocationListener {
  13. LocationManager locationmanager;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  19. Criteria cri=new Criteria();
  20. String provider=locationmanager.getBestProvider(cri,false);
  21. if(provider!=null & !provider.equals(""))
  22. {
  23. Location location=locationmanager.getLastKnownLocation(provider);
  24. locationmanager.requestLocationUpdates(provider,2000,1,this);
  25. if(location!=null)
  26. {
  27. onLocationChanged(location);
  28. }
  29. else{
  30. Toast.makeText(getApplicationContext(),"location not found",Toast.LENGTH_LONG ).show();
  31. }
  32. }
  33. else
  34. {
  35. Toast.makeText(getApplicationContext(),"Provider is null",Toast.LENGTH_LONG).show();
  36. }
  37. }
  38. @Override
  39. public boolean onCreateOptionsMenu(Menu menu) {
  40. // Inflate the menu; this adds items to the action bar if it is present.
  41. getMenuInflater().inflate(R.menu.main, menu);
  42. return true;
  43. }
  44. @Override
  45. public void onLocationChanged(Location location) {
  46. TextView textView2=(TextView)findViewById(R.id.textview2);
  47. TextView textView3=(TextView)findViewById(R.id.textview3);
  48. textView2.setText("Latitude"+location.getLatitude());
  49. textView3.setText("Longitude"+ location.getLongitude());
  50. }
  51. @Override
  52. public void onStatusChanged(String s, int i, Bundle bundle) {
  53. }
  54. @Override
  55. public void onProviderEnabled(String s) {
  56. }
  57. @Override
  58. public void onProviderDisabled(String s) {
  59. }
  60. }
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