How To Get Location In Android Programmatically? - Code Snippets

Steps to get location in Android:

1. Provide permissions in manifest file for receiving location update

to access current location information through location providers, we need to set permissions with Android manifest file.

<manifest ...>

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

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION">

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

</manifest>

ACCESS_COARSE_LOCATION is used when we use network location provider for our Android app.
ACCESS_FINE_LOCATION is providing permission for both providers.
INTERNET permission is must for the use of network provider.

2. Create LocationManager instance as reference to the location service

For any background Android Service, we need to get reference for using it. Similarly, location service reference will be created using getSystemService() method. This reference will be added with the newly created LocationManager instance as follows.

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

3. Request location from LocationManager

After creating the location service reference, location updates are requested using requestLocationUpdates() method of LocationManager. For this function, we need to send the type of location provider, number of seconds, distance and the LocationListener object over which the location to be updated.

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

Java Snippet:

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener{

protected LocationManager locationManager;

protected LocationListener locationListener;

protected Context context;

TextView txtLat;

String lat;

String provider;

protected String latitude,longitude;

protected booleangps_enabled,network_enabled;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

txtLat = (TextView) findViewById(R.id.textview1);

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

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

}

@Override

public void onLocationChanged(Location location){

txtLat = (TextView) findViewById(R.id.textview1);

txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongit());

}

@Override

public void onProviderDisabled(String provider){

Log.d("Latitude","disable");

}

@Override

public void onProviderEnabled(String provider){

Log.d("Latitude","enable");

}

@Override

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

Log.d("Latitude","status");

}

}

XML files for layout and android manifest

<RelativeLayoutxmlns: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"

tools:context=".MainActivity" >

<TextView

android:id="@+id/textview1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

android:text="@string/hello_world" >

</RelativeLayout>

AndroidManifest Files

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.geolocationfinder"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="17">

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

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppBaseTheme">

<activity

android:name="com. android.geolocationfinder.MainActivity"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN">

<category android:name="android.intent.category.LAUNCHER">

</intent-filter>

</activity>

</application>

</manifest>

How to send latitude and longitude to android emulator


1.Open DDMS perspective in Studio (Tools ? Android ? Android Device Monitor).
2.Select your emulator device
3.Select the tab named emulator control
4.In "Location Controls" panel, "Manual" tab, give the Longitude and Latitude as input and "Send".

LocationManager

This class provides access to the system location services. These services allow applications to obtain periodic updates of the device's geographical location, or to fire an application-specified Intent when the device enters the proximity of a given geographical location.You do not instantiate this class directly; instead, retrieve it through

Context.getSystemService(Context.LOCATION_SERVICE).
    

Example

LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);

LocationListener (Interface)
Used for receiving notifications from the LocationManager when the location has changed. These methods are called if the LocationListener has been registered with the location manager service using the requestLocationUpdates(String, long, float, LocationListener) method. 

Abstract Methods

1.public abstract void onLocationChanged (Location location)

Explination:
Called when the location has changed.There are no restrictions on the use of the supplied Location object.

2.public abstract void onProviderDisabled (String provider)

Explination:
Called when the provider is disabled by the user. If requestLocationUpdates is called on an already disabled provider, this method is called immediately.

3.public abstract void onProviderEnabled (String provider)

Explination:
Called when the provider is enabled by the user.

4.public abstract void onStatusChanged (String provider, intstatus,Bundle extras)

Explination:Called when the provider status changes. This method is called when a provider is unable to fetch a location or if the provider has recently become available after a period of unavailability.
Comment via Facebook
0 Comment via Google

0 تعليقات:

Post a Comment