Professional Documents
Culture Documents
CS-6611 / IT-6611
Exercise - 8
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Latitude"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtLat" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Longitude"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 2
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtLong" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Altitude"
android:id="@+id/textView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/txtAlt" />
</LinearLayout>
We have added 6 textViews three textViews as labels and other three to display
values.
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 3
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 4
@Override
public void onLocationChanged(Location location) {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
}
Add the following data members to MainActivity class.
TextView txtLat, txtLong, txtAlt;
//textviews that display latitude, longitude, altitude values
LocationManager manager;
//location manager to acces location service
String provider;
//the location provider. Here the provider would be GPS
//You can also use network provider as provider
//GPS can only be used outdoors
Inside the onCreate() method add:
txtLat = (TextView)findViewById(R.id.txtLat);
txtLong = (TextView)findViewById(R.id.txtLong);
txtAlt = (TextView)findViewById(R.id.txtAlt);
This is used to get the TextViews from the layout into the java platform.
SETTING LOCATION MANAGER
//getting location manager object
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
This will set the default system location service to be used as location manager.
Set the provider to GPS
provider = LocationManager.GPS_PROVIDER;
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 5
GETTING LOCATION FROM PROVIDER
//get location from the provider
Location location = manager.getLastKnownLocation(provider);
manager.requestLocationUpdates(provider, 0, 0, this);
if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed", Toast.LENGTH_SHORT).show();
This will get location from the location manager and the location manager
requests updates for location after min 0 time and min 0 distance and uses
the MainActivity as location listener.
Now call the onLocationChanged method
if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed", Toast.LENGTH_SHORT).show();
Final onCreate Method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
provider = LocationManager.GPS_PROVIDER;
manager.requestLocationUpdates(provider,0,0,this);
if (location != null) {
onLocationChanged(location);
} else
Toast.makeText(this, "Location cannot be accessed",
Toast.LENGTH_SHORT).show();
}
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 6
Now inside the onLocationChanged method set the TextView texts to Latitude,
Longitude and Altitude of the location we got.
@Override
public void onLocationChanged(Location location) {
txtLat.setText(Double.toString(location.getLatitude()));
txtLong.setText(Double.toString(location.getLongitude()));
txtAlt.setText(Double.toString(location.getAltitude()));
}
Final MainActivity class
package com.blogspot.shuttereditz.geolocation;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
LocationManager manager;
//location manager to acces location service
String provider;
//the location provider. Here the provider would be GPS
//You can also use network provider as provider
//GPS can only be used outdoors
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
provider = LocationManager.GPS_PROVIDER;
manager.requestLocationUpdates(provider,0,0,this);
if (location != null) {
onLocationChanged(location);
} else
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai
Android GPS Location 7
@Override
public void onLocationChanged(Location location) {
txtLat.setText(Double.toString(location.getLatitude()));
txtLong.setText(Double.toString(location.getLongitude()));
txtAlt.setText(Double.toString(location.getAltitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
@Override
public void onProviderEnabled(String provider) {
@Override
public void onProviderDisabled(String provider) {
}
}
Testing
While testing you should know that
GPS locating cannot be used indoors.
If using an emulator you need to pass the location values yourself using the
settings of the emulator.
So, it is better to test it in a device outdoor.
Copyright © Arjun P (Arjun Atlast) fb.com/arjunatlast
Copyright (c) LinuXpert Systems, Chennai