Showing posts with label Get Current Location (GPS Position) in Android Native Java Code. Show all posts
Showing posts with label Get Current Location (GPS Position) in Android Native Java Code. Show all posts

Thursday, May 26, 2011

Get Current Location (GPS Position) in Android Native Java Code

Get Current Location (GPS Position) in Android Native Java Code

Generally till now while developing Android Apps or I should say PhoneGap Supported Apps, to get the current GPS location, I always referred to the following function:

navigator.geolocation.getCurrentPosition(onSuccess, onError);

which instantly gives all the required location details like latitude, longitude, accuracy etc.

But then, how do I get these details which I'm not using a web app or I should say not a PhoneGap Application but a pure native Android Application or an Android Service, So for the same I tried researching  on the various articles and documents and finally arrived at the solution as follows:

package com.mayuri.location;
import java.sql.Timestamp;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log; 
import android.widget.TextView;
import android.widget.Toast;


public class LocationSample extends Activity {   
TextView tv;
    @Override 
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);         
    tv = (TextView)this.findViewById(R.id.txtLocation);
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new mylocationlistener();
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);    
    }  
    private class mylocationlistener implements LocationListener {
    @Override 
    public void onLocationChanged(Location location) {    
     Date today = new Date();  
Timestamp currentTimeStamp = new Timestamp(today.getTime());
        if (location != null) {
        Log.d("LOCATION CHANGED", location.getLatitude() + "");
        Log.d("LOCATION CHANGED", location.getLongitude() + "");
        Strig str = "\n CurrentLocation: "+
        "\n Latitude: "+ location.getLatitude() + 
        "\n Longitude: " + location.getLongitude() + 
        "\n Accuracy: " + location.getAccuracy() + 
        "\n CurrentTimeStamp "+ currentTimeStamp;         
          Toast.makeText(LocationSample.this,str,Toast.LENGTH_LONG).show();
          tv.append(str);               
        } 
    } 
    @Override
    public void onProviderDisabled(String provider) {
    Toast.makeText(LocationSample.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show();
    }    
    @Override
    public void onProviderEnabled(String provider) {
    Toast.makeText(LocationSample.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(LocationSample.this,"onStatusChanged",Toast.LENGTH_LONG).show();
    }
    }
}

The heart of the Logic lies in the 'LocationManager' class, which provides an access to the System location services.  

'requestLocationUpdates' gives the periodic location updates as per the arguments specified.


The method, 'onLocationChanged' of the Interface 'LocationListener' gives the entire location details like Latitude, Longitude, Accuracy etc which can be used as per our requirements, currently In the example I'm periodically updating the text view as well as showing the Toast Popup of the location details along with the Current Timestamp.

Most Important thing to be remembered while using the LocationService in Android Application is to modify the Application manifest file with the following,

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


Thats all on this from my end!!

Happy Coding :-))

~ Mayuri



Best Car pool Android Application Best Car Pool Application Source Code Download Here



Best Social Network Cordova Ionic Application Best Social Networking Source Code Cordova Ioinc Application Download Here


Best Android Application, Android Restaurant System Best Android Restaurant Application Source code download here


Best Android Application Android Chat Source code download Android Chat Source code download


Best Android Quiz Source Code Download Best Android Quiz Source Code Download here

More and Lots of useful Android source codes here Best Android Source codes download here


Multi-Agent Orchestration in Vibe Coding: How I Built Chai & Chapters with Google Antigravity

If you have been following my tech journey on Catch Mayuri, you know how passionate I am about exploring the absolute cutting edge of web de...