Obtaining user location

To obtain user location, retrieve LocationManager by calling getSystemService(Context.LOCATION_SERVICE). By the LocationManager, you can registers LocationListener to be notified periodically based on the specified criteria, by the named provider(PROVIDER in the code). And also obtain last known location.

Obtaining user location


For PROVIDER of GPS_PROVIDER, determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. For PROVIDER of NETWORK_PROVIDER,  determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup.

Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)

AndroidLocationActivity.java
package com.exercise.AndroidLocation;

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.widget.TextView;

public class AndroidLocationActivity extends Activity {

String PROVIDER = LocationManager.GPS_PROVIDER;
//String PROVIDER = LocationManager.NETWORK_PROVIDER;

LocationManager locationManager;
double myLatitude, myLongitude;

TextView textLatitude, textLongitude;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textLatitude = (TextView)findViewById(R.id.Latitude);
textLongitude = (TextView)findViewById(R.id.Longitude);

locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location lastLocation = locationManager.getLastKnownLocation(PROVIDER);
if(lastLocation != null){
updateLoc(lastLocation);
}
}

private void updateLoc(Location loc){
textLatitude.setText("Latitude: " + loc.getLatitude());
textLongitude.setText("Longitude: " + loc.getLongitude());
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationManager.requestLocationUpdates(PROVIDER, 0, 0, myLocationListener);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(myLocationListener);
}



private LocationListener myLocationListener
= new LocationListener(){

@Override
public void onLocationChanged(Location location) {
updateLoc(location);

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}};

}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>


Modify AndroidManifest.xml to add permission of "android.permission.ACCESS_FINE_LOCATION".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidLocation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidLocationActivity"
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>


Download the files.

Next:
- Location updates from GPS_PROVIDER and NETWORK_PROVIDER

0 Response to "Obtaining user location"

Post a Comment