In this GPS tutorial you will learn how to get current location in android.
LocationManager class provides the facility to get latitude and longitude coordinates of current location. The class in which you want to get location should implement LocationListener and override all its abstract methods.
How to Get Current Location in Android Using Location Manager
Add following permissions to AndroidManifest.xml file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" />
Following lines of code helps in getting current location.
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
In requestLocationUpdates() method the 2nd argument is time in milliseconds and 3rd argument is distance in meters. Here I have used 5000 and 5 that means after every 5 seconds and 5 meter the current location is fetched.
To get coordinates use getLatitude() and getLongitude() method on variable of Location class.
Whenever location is changed it can be fetched inside onLocationChanged() method.
Below is simple application that fetches current location on button click and displays it in textview.
Full Source Code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.currentlocation.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get Current Location" android:id="@+id/getLocationBtn"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/locationText" android:layout_below="@id/getLocationBtn"/> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.currentlocation"> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.currentlocation; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements LocationListener { Button getLocationBtn; TextView locationText; LocationManager locationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getLocationBtn = (Button)findViewById(R.id.getLocationBtn); locationText = (TextView)findViewById(R.id.locationText); getLocationBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getLocation(); } }); } void getLocation() { try { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this); } catch(SecurityException e) { e.printStackTrace(); } } @Override public void onLocationChanged(Location location) { locationText.setText("Current Location: " + location.getLatitude() + ", " + location.getLongitude()); } @Override public void onProviderDisabled(String provider) { Toast.makeText(MainActivity.this, "Please Enable GPS and Internet", Toast.LENGTH_SHORT).show(); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } }
Screenshot
Comment below if found anything incorrect or have doubts related to above android gps tutorial to get current location.
I’m studying 2nd yr cs engineering . will yu plz give some idea to do mini project in c,c++…..
Follow below link, there you will get many project ideas.
http://www.codewithc.com/c-projects-with-source-code/
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
it works if you add NETWORK_PROVIDER
Hi,
When i click the buton it show nothing lang and lat…but i add Toast between this,
Toast.makeText(getApplicationContext(), “Your Location”, Toast.LENGTH_LONG).show();
getLocation();
Toast.makeText(getApplicationContext(), “Your Location showing”, Toast.LENGTH_LONG).show();
But,Am able to find long and lat.
Advance Thanks
Hi
how do you solve it please ,i add Toast but nothing change
plz tell us where we needed to insert these toast..we are unable to do correct thsi
after clicking button it is not displaying any location
Makes sure the gps is one, test it in real android device.
Hello Neeraj!
I am running the tutorial code on a real device and gps is on but onLocationChanged is never called. I was wondering what version of the sdk you are running?
Thanks!
code not working
Use This Code:
if(checkLocationPermission()) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.e(“Current Location”,”lat: “+location.getLatitude()+” Lon: “+location.getLongitude());
}
this code is properly work.nycccc and easy code for get loacation.thank you
your code is not working
just button is getting displayed
when i clicked on that button,nothing got displayed
how to get current location not like above.. means current location place name should be displayed
its not working
It is not working
code is not working
I am running the tutorial code on a real device and gps is on but onLocationChanged is never called.
Hi
For this to work on my phone(Nougat). I needed to go to Settings, Location Services, App-Level Permission. My newly deployed app appeared in the list. I needed toggle the on button.
Then go back to your app, press the button, wait a few seconds and the location appears
Hope this helps.
thanx man.
.
.
your comment is really helpful
Thank you. This works. The problem at the above code is they are not asking for permission. You must give the permission manually.
your code is not working
just button is getting displayed
when i clicked on that button (GET CURRENT LOCATION),nothing got displayed, i have already GPS on
In above code you displaying the current location of your.,
But what if we want to find the location of other…
can you code for that???
Many Thanks sir,
It is working fine…We have to add device permission dynamically for latest versions of android…
Hi how to give device pemission. me too working on this code.. please help me.
Hi thank you for sharing. i need to get current location. but i click button i didn’t get any values.
am trying 1 week but i can’t get any code. please help me.
Hi, this code works accurately fine. This was not working on emulator. I installed this app in my mobile device and still it was not working. Then as Matt suggested above, I went to Settings of my mobile and typed Permissions in search box to access Permission Settings. There I opened App Permissions options and in App Permissions options I opened Your Location Permissions. There I saw all installed Apps of my device. I just turned on Your Location Permission for this app. Then I turned on GPS and ran the app. It worked.
Also be careful about package name while pasting the above code in your code.
Regards,