Home Coding Android Coding How to Get the Current Location in Android

How to Get the Current Location in Android

1

gps_locations

This post will feature a simple Android application that will display the current location in latitude and longitude. This is achieved with the help of the GPS function and the LocationManager API. You can follow the steps down below to recreate an app in Android Studio that is capable of fetching the current location and display it on the screen.

How to Get the Current Location in Android Studio

1. Create a New Android Studio Project.

1

2. Open SDK Manager and check whether you have Google Play Services installed. If not, go to SDK Tools and download Google Play Services.

2

3. Add the following code into AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appdosh.location"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="4"
        android:targetSdkVersion="15" />
    
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />    
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.appdosh.location.MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.  Insert the following code into MainActivity.java:

package com.appdosh.gpslocation;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener{
 
 LocationManager locationManager ;
 String provider;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 // Get LocationManager
 locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
 
 // Create empty criteria object
 Criteria criteria = new Criteria();
 
 // Get provider name that matches criteria
 provider = locationManager.getBestProvider(criteria, false);
 
 if(provider!=null && !provider.equals("")){
 
 // Get the location from provider 
 Location location = locationManager.getLastKnownLocation(provider);
 // Start the location listener and run for 30 seconds to get GPS. You can increase it. 
 locationManager.requestLocationUpdates(provider, 30000, 1, this);
 
 if(location!=null)
 onLocationChanged(location);
 else
 Toast.makeText(getBaseContext(), "Unable to retrieve location.", Toast.LENGTH_SHORT).show();
 
 }else{
 Toast.makeText(getBaseContext(), "Provider Not Found", Toast.LENGTH_SHORT).show();
 }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.activity_main, menu);
 return true;
 }
 
 @Override
 public void onLocationChanged(Location location) {
 // Referring to TextView textview_mylongitude
 TextView tvLongitude = (TextView)findViewById(R.id.tv_mylongitude);
 
 // Referring to TextView textview_myatitude
 TextView tvLatitude = (TextView)findViewById(R.id.tv_mylatitude);
 
 // My Current Longitude
 tvLongitude.setText("Current Longitude:" + location.getLongitude());
 
 // My Current Latitude
 tvLatitude.setText("Current Latitude:" + location.getLatitude() );
 }

 @Override
 public void onProviderDisabled(String provider) {
 // TODO Auto-generated method stub 
 }

 @Override
 public void onProviderEnabled(String provider) {
 // TODO Auto-generated method stub 
 }

 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
 // TODO Auto-generated method stub 
 }
}

5.  Add the following code into activity_main.xml :

<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" >
    
    <TextView
        android:id="@+id/tv_mylocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/get_tv_mylocation"
        android:textStyle="normal|bold" />
    
    <TextView
        android:id="@+id/tv_mylongitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_mylocation"
        android:layout_centerHorizontal="true" />
    
    <TextView
        android:id="@+id/tv_mylatitude"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_mylongitude"
        android:layout_centerHorizontal="true"/>    

</RelativeLayout>

6. This is how our strings file looks like:

<resources>
<string name="app_name">AppDosh Current Location</string>
<string name="hello_world">Hello world!</string>
<string name="settings">Settings</string>
<string name="title_activity_main">Location from GPS</string>
<string name="get_tv_mylocation">Current Location</string>
</resources>

7. Run the app. It should display the location in latitude and longitude.

13499939_1737263879845231_1239540512_o

You can download the source code by clicking the link below.

Download Source Code
Denis Madroane App junkie, Android developer, proud Freelancer. Passionate about app development and blogging, updated with the new stuff. Likes to think that he's versed in app re-skinning, content writing and social media.

Comment(1)

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *

Powered by WordPress