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.
The post How to Get Current Location in Android Using Location Manager appeared first on The Crazy Programmer.