Android獲取GPS坐標:
- package an.android.application;
-
-
- import java.util.Iterator;
-
- import android.app.Activity;
- import android.content.Intent;
-
- import android.location.GpsSatellite;
- import android.location.GpsStatus;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
-
- public class SpeedToll extends Activity {
- /** Called when the activity is first created. */
-
-
- private static final int Search = Menu.FIRST;
- private static final int Myloc = Menu.FIRST + 1;
- private static final int Exit = Menu.FIRST + 2;
-
-
- private LocationManager locationManager;
- private GpsStatus gpsstatus;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
- /* 加載main.xml Layout */
- setContentView(R.layout.desktop);
- /* 以findViewById()取得Button對象,並加入onClickListener */
- Button b1 = (Button) findViewById(R.id.button1);
- b1.setOnClickListener(new Button.OnClickListener()
- {
- public void onClick(View v)
- {
- /* new一個Intent對象,並指定要啟動的class */
- Intent intent = new Intent();
- intent.setClass(SpeedToll.this, Map.class);
- /* 調用一個新的Activity */
- startActivity(intent);
- /* 關閉原本的Activity */
- //SpeedToll.this.finish();
- }
- });
-
-
- Button b2 = (Button) findViewById(R.id.button1);
- b2.setOnClickListener(new Button.OnClickListener()
- {
- public void onClick(View v)
- {
- /* new一個Intent對象,並指定要啟動的class */
- Intent intent = new Intent();
- intent.setClass(SpeedToll.this, Map.class);
- /* 調用一個新的Activity */
- startActivity(intent);
- /* 關閉原本的Activity */
- //SpeedToll.this.finish();
- }
- });
- }
-
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- menu.add(0, Search, Menu.NONE, "搜索");
- menu.add(0, Myloc, Menu.NONE, "定位");
- menu.add(0, Exit, Menu.NONE, "退出");
-
-
- return true;
- }//menu
-
- public boolean onOptionsItemSelected(MenuItem item)
- {
- //super.onOptionsItemSelected(item);
- switch(item.getItemId()){
- case Search:
- //Search();
- break;
-
- case Myloc:
- GetMyLocation();
- break;
-
- case Exit:
- //mLocationManager.removeUpdates(this); //關閉GPS
-
- this.finish();
- break;
- }
- return super.onOptionsItemSelected(item);
- }
-
-
- public boolean GetMyLocation(){
- //獲取到LocationManager對象
- locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
-
- //根據設置的Criteria對象,獲取最符合此標准的provider對象
- String currentProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
-
- //根據當前provider對象獲取最後一次位置信息
- Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
- //如果位置信息為null,則請求更新位置信息
- if(currentLocation == null){
- locationManager.requestLocationUpdates(currentProvider, 0, 0, locationListener);
- }
- //增加GPS狀態監聽器
- locationManager.addGpsStatusListener(gpsListener);
-
- //直到獲得最後一次位置信息為止,如果未獲得最後一次位置信息,則顯示默認經緯度
- //每隔10秒獲取一次位置信息
- while(true){
- currentLocation = locationManager.getLastKnownLocation(currentProvider);
- if(currentLocation != null){
- Log.d("Location", "Latitude: " + currentLocation.getLatitude());
- Log.d("Location", "location: " + currentLocation.getLongitude());
- Toast.makeText(SpeedToll.this, "Latitude: " + currentLocation.getLatitude()+ " "
- +"location: " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
- break;
- }else{
- Log.d("Location", "Latitude: " + 0);
- Log.d("Location", "location: " + 0);
- }
- try {
- Thread.sleep(10000);
- } catch (InterruptedException e) {
- Log.e("Location", e.getMessage());
- }
- }
- return false;
- }
-
- private GpsStatus.Listener gpsListener = new GpsStatus.Listener(){
- //GPS狀態發生變化時觸發
- @Override
- public void onGpsStatusChanged(int event) {
- //獲取當前狀態
- gpsstatus=locationManager.getGpsStatus(null);
- switch(event){
- //第一次定位時的事件
- case GpsStatus.GPS_EVENT_FIRST_FIX:
- break;
- //開始定位的事件
- case GpsStatus.GPS_EVENT_STARTED:
- break;
- //發送GPS衛星狀態事件
- case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
- Toast.makeText(SpeedToll.this, "GPS_EVENT_SATELLITE_STATUS", Toast.LENGTH_SHORT).show();
- Iterable<GpsSatellite> allSatellites = gpsstatus.getSatellites();
- Iterator<GpsSatellite> it=allSatellites.iterator();
- int count = 0;
- while(it.hasNext())
- {
- count++;
- }
- Toast.makeText(SpeedToll.this, "Satellite Count:" + count, Toast.LENGTH_SHORT).show();
- break;
- //停止定位事件
- case GpsStatus.GPS_EVENT_STOPPED:
- Log.d("Location", "GPS_EVENT_STOPPED");
- break;
- }
- }
- };
-
-
- //創建位置監聽器
- private LocationListener locationListener = new LocationListener(){
- //位置發生改變時調用
- @Override
- public void onLocationChanged(Location location) {
- Log.d("Location", "onLocationChanged");
- }
-
- //provider失效時調用
- @Override
- public void onProviderDisabled(String provider) {
- Log.d("Location", "onProviderDisabled");
- }
-
- //provider啟用時調用
- @Override
- public void onProviderEnabled(String provider) {
- Log.d("Location", "onProviderEnabled");
- }
-
- //狀態改變時調用
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- Log.d("Location", "onStatusChanged");
- }
-
-
- };
-
- }
主要有兩個文件,一個是主文件,一個是xml文件。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11