Monday, September 19

Code to Access GPS in Android Phones

GET !!! SET !! GO..!!!!!!!!

Location Based Services (LBS) have become very popular in past few years after the advent of GPS enabled smartphone devices . In this post, we shall see how a LBS application for Android works.

Simply speaking map navigation is possible in  following ways:
  1. Using the GPS device in built in mobile-    Easier and precise.
  2. Using the ID of the Cell that the user is currently served by



How to code?
We can give dummy values as GPS co-ordiantes for a location(i.e., Longitude and Latitude) .Another way to emulate location is to including .gpx files. Although it is not very flexible , it can give better performance.

Pre-requisite

First include this code fragment in AndroidManifest.xml to access GPS:

 
<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.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.INTERNET"/>




Application Code


 
import com.GPS_project.android.lbs.R;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class gps extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

protected LocationManager locationManager;

protected Button retrieveLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

retrieveLocationButton = (Button)findViewById(R.id.retrieve_location_button);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

 locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);

retrieveLocationButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showCurrentLocation();
}
});

}

protected void showCurrentLocation() {

Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (location != null) {
String message = String.format(
"Current Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());

Toast.makeText(gps.this, message,
Toast.LENGTH_LONG).show();
}
if (location == null)
{
Toast.makeText(gps.this, "No Location Available ", Toast.LENGTH_LONG).show();
}

}

private class MyLocationListener implements LocationListener {

public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
Toast.makeText(gps.this, message, Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(gps.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}

public void onProviderDisabled(String s) {
Toast.makeText(gps.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(gps.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}

}

}



8 comments :

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hi bro,
thanks for such nice tutorials.
I am actually new to android and I am learning it online.
Bro, I would like to learn how to write an application about finding the nearby food place dynamically.

I think I have asked too much.. :D
Please help me ...
Thank you

Regards,
Shiva

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous

Hello Shiva,
it is nice to hear that u liked my Blog.Thanks dude..!!

It is my pleasure to help you if I can. please mail me what you want to know so that I can help you.!! :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hi bro,
I wished I could mail you but I couldn't able to mail to you since I haven't created an email account using microsoft outlook. May I know your email address? Mine is shiva.26july@gmail.com
Thanks for your compliments, bro.

Regards,
Shiva

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

You can contact me at
vipinnarayantvm@
gmail.com
all other contact details
are provided at the menu
bar near the Blog title as
"CONTACT ME"..!!!

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Thanks bro....
See you in gmail....


Regards,
Shiva

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

Hi Bro,
I am Shiva. May I ask you a few things regarding about android applications? How to do so that the application could display near by restaurants? What are the essential requirements to implement this? So far I only know that I will need Google map and overlay.
I would be grateful if you could help me with this, bro.
Thank you very much.

Regards,
Shiva

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

hi vipin :)
yr blog seems very interesting.... it makes me want to create an app :)
i would like to make an application that finds the restaurants in nearby location using gps technology /.. need yr help and will be greatful to know the application codes..
thanks in advance :)

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

kadakkkkk!!!!!

Post a Comment