<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/lonText"
>
</TextView>
<TextView
android:text="unknown"
android:id="@+id/longitutdeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/latText"
>
</TextView>
<TextView
android:text="unknown"
android:id="@+id/latitudeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
</LinearLayout>
Copy source code berikut pada file MainActivity.java di folder src.
package com.example.pengenalangps;
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;
import android.widget.Toast;
public class MainActivity extends Activity {
private LocationManager lm;
private LocationListener locListener;
private TextView latTxt,lonTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latTxt = (TextView) findViewById(R.id.latitudeTxt);
lonTxt = (TextView) findViewById(R.id.longitutdeTxt);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 200, locListener);
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
latTxt.setText(String.valueOf(loc.getLatitude()));
lonTxt.setText(String.valueOf(loc.getLongitude()));
Toast.makeText(getBaseContext(), "Location Changed : Lat " + loc.getLatitude() + "lgt: "+loc.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
public void onProviderDisabled(String arg0) {}
public void onProviderEnabled(String arg0) {}
public void onStatusChanged(String provider, int status, Bundle extras){}
}
}
Jalankan emulator dan akan tampil seperti gambar di bawah ini.
Jika belum tampil informasi Longitude dan Latitude, lakukan langkah berikut:
1. Pada menu bar, pilih Window>Open Perspective>DDMS
2. Pilih Emulator Control.
3. Pada Location Controls, isikan nilai untuk longitude dan latitude. Kemudian klik tombol Send.
Tidak ada komentar:
Posting Komentar