2013/11/03

Android 透過網路或GPS取得當前座標


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

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />
    <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.getlocation.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>

Layout:
<RelativeLayout 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: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" >

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

    <Button
        android:id="@+id/buttonNetwork"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="18dp"
        android:text="網路" />

    <Button
        android:id="@+id/buttonGPS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonNetwork"
        android:layout_alignBottom="@+id/buttonNetwork"
        android:layout_centerHorizontal="true"
        android:text="GPS" />

    <Button
        android:id="@+id/buttonStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonGPS"
        android:layout_alignBottom="@+id/buttonGPS"
        android:layout_alignParentRight="true"
        android:layout_marginRight="28dp"
        android:text="Stop" />

</RelativeLayout>



Code:
package com.example.getlocation;

import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 Button btn_Network;
 Button btn_GPS;
 Button btn_Stop;
 TextView textView;
 LocationManager locationManager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // Set Object
  btn_GPS = (Button) findViewById(R.id.buttonGPS);
  btn_Network = (Button) findViewById(R.id.buttonNetwork);
  btn_Stop = (Button) findViewById(R.id.buttonStop);
  textView = (TextView) findViewById(R.id.textView1);

  // Set Service
  locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

  // Add listener
  btn_GPS.setOnClickListener(listenerGPS);
  btn_Network.setOnClickListener(listenerGPS);
  btn_Stop.setOnClickListener(listenerStop);
 }

 private Button.OnClickListener listenerStop = new OnClickListener() {
  @Override
  public void onClick(View v) {
   locationManager.removeUpdates(locationListener);
  }
 };

 private Button.OnClickListener listenerGPS = new OnClickListener() {
  @Override
  public void onClick(View v) {
   String commadStr = null;

   switch (v.getId()) {
   case R.id.buttonGPS:
    commadStr = LocationManager.GPS_PROVIDER;
    break;
   case R.id.buttonNetwork:
    commadStr = LocationManager.NETWORK_PROVIDER;
    break;
   }

   locationManager.requestLocationUpdates(commadStr, 0, 0,
     locationListener);
  }
 };

 private LocationListener locationListener = new LocationListener() {
  public void onLocationChanged(Location location) {
   /*
    * Called when a new location is found by the network location
    * provider.
    */
   textView.setText("經度:" + location.getLongitude() + ", 緯度:"
     + location.getLatitude());
  }

  public void onStatusChanged(String provider, int status, Bundle extras) {
  }

  public void onProviderEnabled(String provider) {
  }

  public void onProviderDisabled(String provider) {
  }
 };

}



參考資料:
http://developer.android.com/guide/topics/location/strategies.html
http://developer.android.com/reference/android/location/LocationManager.html
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(long, float, android.location.Criteria, android.app.PendingIntent)
http://developer.android.com/reference/android/location/Location.html