Kamis, 10 Januari 2013

Get location from GPS

Android system provide android.location.LocationManager class to access 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).

All Location API methods require the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions. If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.

Example:
Get location from GPS


package com.example.androidmylocation;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView testViewStatus, textViewLatitude, textViewLongitude;

LocationManager myLocationManager;
String PROVIDER = LocationManager.GPS_PROVIDER;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testViewStatus = (TextView)findViewById(R.id.status);
textViewLatitude = (TextView)findViewById(R.id.latitude);
textViewLongitude = (TextView)findViewById(R.id.longitude);

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

//get last known location, if available
Location location = myLocationManager.getLastKnownLocation(PROVIDER);
showMyLocation(location);
}

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

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

private void showMyLocation(Location l){
if(l == null){
testViewStatus.setText("No Location!");
}else{
textViewLatitude.setText("Latitude: " + l.getLatitude());
textViewLongitude.setText("Longitude: " + l.getLongitude());
}

}

private LocationListener myLocationListener
= new LocationListener(){

@Override
public void onLocationChanged(Location location) {
showMyLocation(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

}};

}


<LinearLayout xmlns: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"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<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>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidmylocation"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<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/AppTheme" >
<activity
android:name="com.example.androidmylocation.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>


Related:
- Get location from mobile or WIFI networks


0 komentar:

Posting Komentar

Copyright © 2012 Codding News All Right Reserved