Android: Get user’s current location using GPS


Step 1: create new project
Step 2: In file AndroidManifest.xml

After line:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

Add:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Step 3: right click com.example.myapplication -> New -> Java Class with name GPS

android new java class

package com.example.myapplication;

import android.app.Service;
import android.content.Context;
import android.content.Intent;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by Tutorialspots.com on 12/27/2015.
 */
public class GPS extends Service implements LocationListener {

    private final Context context;
    public boolean isGPSEnabled = false;
    public boolean isNetworkEnable = false;
    public boolean canGetLocation = false;

    public double longitude;
    public double latitude;

    public Location location;

    private static final long MIN_DISTANCE = 10;
    private static final long MIN_TIME = 60000; //ms = 1 minute

    protected LocationManager locationManager;

    public GPS(Context context) {
        this.context = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (isGPSEnabled || isNetworkEnable) {
                this.canGetLocation = true;
                if (isNetworkEnable) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME,
                            MIN_DISTANCE, this);
                }
                if(locationManager != null){
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(location != null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

                if(isGPSEnabled && location == null){
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME,
                            MIN_DISTANCE,this);
                    if(locationManager != null){
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if(location != null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }else{

            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return location;
    }

    public double getLongitude(){
        if(location != null){
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public boolean canGetLocation(){
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Step 4: in file content_main.xml add

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textColor="#ff0000"
        android:text="TUTORIALSPOTS.COM" />

    <Button
        android:id="@+id/btn"
        android:text="Show your location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

Step 5: in file MainActivity.java

After line public class MainActivity extends AppCompatActivity { add

    public Button btn;
    public GPS gps;

in method onCreate add:

btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                gps = new GPS(MainActivity.this);
                if(gps.canGetLocation()){
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                    Toast.makeText(getApplicationContext(),"Lat: " + latitude + ";\nLong: " + longitude + ";",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(),"Can't get Location by using GPS;",Toast.LENGTH_LONG).show();
                }
            }
        });

Done, now you can test your application.

Step 6: Start a genymotion virtual device

Then click icon Run and select your virtual device then click OK.

emulate with genymotion

Step 7: Genymotion set GPS location:

Click icon GPS then you can set your location you like. Then close this dialog.

genymotion set gps location

Result:

genymotion for gps location

Leave a Reply