Monday, November 28, 2011

Gps in Android

GPS(Global Positioning System):-
·         Most Android devices allow to determine the current geolocation. This can be done via a GPS (Global Positioning System) device, via cell tower triangulation or via wifi networks for which the geolocation is known. Android provides the package "android.location" which provides the API to determine the current geo position.

·         You can utilize GPS and Android's Network Location Provider to acquire the user location. Although GPS is most accurate, it only works outdoors, it quickly consumes battery power, and doesn't return the location as quickly as users want.

·         Android's Network Location Provider determines user location using cell tower and Wi-Fi signals, providing location information in a way that works indoors and outdoors, responds faster, and uses less battery power

·         The class "LocationManager" provides access to the location service . The Android device might have several providers available and you can select one of them. For a flexible selection of the best location provider use a "Criteria" object in which you can define how the provider should be selected.

·         You can register a "LocationListener" with the "LocationManager" and will receive periodic updates about the geoposition. The class "LocationProvider" is the superclass of the different location providers which deliver the information about the current location

 Requesting Location Updates
// Acquire a reference to the system Location Manager

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

// Define a listener that responds to location updates

LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates     , 0, 0, locationListener);

Remove Updates

Requesting User Permissions
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Note: Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER
To test in Emulator using Telnet
telnet  localhost  port
geo fix latitude longitude altitude
Ex: geo fix -121.45356 46.51119 4392

The class "Geocoder" allow to determine the geo-coordinates (longitude, laditude) for a given address and possible addresses for given geo-coordinates. This process is known as forward and reverse geocoding.
After getting instance fot the Geocoder, use this instance(object) to call the getFromLocation method by passing three parameters(1-Latitude 2-Longtitude,3-No of Results) and it will return list of results.
Parse the list to obtain the physical address of given co-ordinates

Geocoder myLocation = new Geocoder(this, Locale.getDefault());
List myList = myLocation.getFromLocation(17.38184921434858, 78.48134994506836, 1);