請先AndroidManifest.xml設定權限

1.允許使用網路權限

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

2.允許使用GPS權限

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

3.允許使用網路定位權限

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

ps 請記得開啟手機GPS

device045  device046  

小試身手小範例

java檔

public class getGPS extends Activity implements LocationListener {
private LocationManager locationManager;
private LocationListener locationListener;
TextView textView01;
TextView textView02;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView01 = (TextView)findViewById(R.id.TextView01);
textView02 = (TextView)findViewById(R.id.TextView02);
UpdateLocation();
}
public void UpdateLocation(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
public void onLocationChanged(Location newLocation) {
Double longitude = newLocation.getLongitude() * 1000000;
Double latitude = newLocation.getLatitude() * 1000000;
Log.i("Location=", "X=" + longitude.intValue() + ", Y=" + latitude.intValue());
textView01.setText("經度"+longitude);
textView02.setText("緯度"+latitude);
}
@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

}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Double longitude = location.getLongitude() * 1000000;
Double latitude = location.getLatitude() * 1000000;
Log.i("Location=", "X=" + longitude.intValue() + ", Y=" + latitude.intValue());
textView01.setText("經度"+longitude);
textView02.setText("緯度"+latitude);
}
@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

}
}

XML檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="抓取中GPS ing"
android:id="@+id/TextView01"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="抓取中GPS ing"
android:id="@+id/TextView02"
/>
</LinearLayout>

AndroidManifest檔

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sam.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".getGPS"
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>

 

arrow
arrow

    S 發表在 痞客邦 留言(8) 人氣()