2012年3月22日 星期四

Location GPS



在 Manifest.xml 加上:

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

Main.java:

//如果沒有加上 implements LocationListener;就得實作 mLocationListener 介面

public class Cq_LocationGPSActivity extends Activity implements LocationListener{
    /** Called when the activity is first created. */
private LocationManager mLocationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
        TextView tv08 = (TextView)findViewById(R.id.textView8);
        tv08.setText("LOCATION-GPS");
    }
    
    

@Override
protected void onResume() {
// TODO Auto-generated method stub
if (mLocationManager != null){
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
super.onResume();
}



@Override
protected void onPause() {
// TODO Auto-generated method stub
if (mLocationManager != null){
mLocationManager.removeUpdates(this);
}
super.onPause();
}



@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
TextView tv1 = (TextView)findViewById(R.id.textView1);
TextView tv2 = (TextView)findViewById(R.id.textView2);
TextView tv3 = (TextView)findViewById(R.id.textView3);
TextView tv4 = (TextView)findViewById(R.id.textView4);
TextView tv5 = (TextView)findViewById(R.id.textView5);
TextView tv6 = (TextView)findViewById(R.id.textView6);
TextView tv7 = (TextView)findViewById(R.id.textView7);
tv1.setText("Latitude :  " + String.valueOf(location.getLatitude()));
tv2.setText("Longitude : " + String.valueOf(location.getLongitude()));
tv3.setText("Accuracy :" + String.valueOf(location.getAccuracy()));
tv4.setText("Latitude : " + String.valueOf(location.getAltitude()));
tv5.setText("Time : " + String.valueOf(location.getTime()));
tv6.setText("Speed : " + String.valueOf(location.getSpeed()));
tv7.setText("Bearing : " + String.valueOf(location.getBearing()));
}

@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
switch (status){
case LocationProvider.AVAILABLE:
Log.v("Status", "AVALIABLE");
break;
case LocationProvider.OUT_OF_SERVICE:
Log.v("Status", "OUT_OF_SERVICE");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v("Status", "TEMPORARILY_UNAVAILABLE");
break;
}
}
}

**********************************
res/layout/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:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    

</LinearLayout>



沒有留言:

張貼留言