請先在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
小試身手小範例
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>

*****
不好意思,可以在仔細說明一下哪邊串錯了嗎? 我看不大懂><"
您好,因為學習上的需求,我必須要使用Android手機,擷取使用者的GPS及地點照片,並且回傳到自己的Server上去,做行為模式的分析。 經過Google後看到您的教學文章,不知是否方便跟您請教,因我本身是Android app的初學者,對於Java也是。THS Mail:dickluo.fama@gmail.com
我照著程式碼貼上去,程式也0 error了,但跑AVD時一直顯示:Unfortunately,test01 has stopped. 不知發生什麼問題?
我都是用手機去測,使用模擬器開發太常遇到無法掌握的問題,這些程式我都有在手機上測試成功才會放上來的唷。
版主大大!!我複製了你的程式碼修改之後 雖然可以執行 可是在手機上面,只有跑出,正在讀取GPSing就沒理我了...我試了三台手機+一台平板都一樣 可以請大大幫我看一下我哪邊出錯了嗎? https://drive.google.com/file/d/0B6-WS8IHZbA9cUJSODNWbVNFMGM/edit?usp=sharing 這是我修改過後的檔案 (手機有開GPS、網路)
你在室內側還是室外? 每台手機抓到的GPS時間不一樣,之前我等了好一下子,或許你可以用其他輔助加速GPS的程式
版主大大!!! 有沒有定位範圍的程式範例的程式? 數學很爛不知道要怎麼算
版主你好: 你的Double longitude = location.getLongitude() * 1000000; Double latitude = location.getLatitude() * 1000000; 為什麼要去* 1000000呢?
我也是只有跑出 正在讀取GPSing 之後就沒有反應了 是不是還有需要做其他修改呢?