Android中GPS定位的用法實例
GPS定位是目前很多手機都有的功能,而且非常實用。本文以實例形式講述了Android中GPS定位的用法。分享給大家參考之用。具體方法如下:
一般在Android中透過GPS取得目前位置,首先要取得一個LocationManager實例,透過該實例的getLastKnownLocation()方法取得第一個的位置,該方法的說明如下:
void android.location.LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
provider即定位方式,可以採用GPS定位(LocationManager.GPS_PROVIDER)或網路定位(LocationManager.NETWORK_PROVIDER),本文為GPS定位,因此使用LocationManager.GPS_PROVIDER。 minTime是位置更新的間隔時間。 listener是位置改變的監聽器,自己定義一個LocationListener(),重寫onLocationChanged(),加入位置改變時的動作。
版面配置檔案如下:
<LinearLayout 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:orientation="vertical" 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/txt_time" style="@style/my_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="时间:" /> <TextView android:id="@+id/txt_lat" style="@style/my_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="经度:" /> <TextView android:id="@+id/txt_lng" style="@style/my_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="纬度:" /> </LinearLayout>
MainActivity.java檔案如下:
package com.hzhi.my_gps; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Timer; import java.util.TimerTask; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.content.Context; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { TextView txt_time; TextView txt_lat; TextView txt_lng; LocationManager lom; Location loc; Double lat; Double lng; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now; String str_date; Timer timer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); get_con(); get_gps(); timer = new Timer(true); timer.schedule(task, 0, 1000); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void get_gps(){ lom = (LocationManager) getSystemService(Context.LOCATION_SERVICE); loc = lom.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (loc != null) { lat = loc.getLatitude(); lng = loc.getLongitude(); txt_lat.setText("纬度:" + String.valueOf(lat)); txt_lng.setText("经度:" + String.valueOf(lng)); } else{ txt_lat.setText("纬度:未知" ); txt_lng.setText("经度:未知" ); } Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); String provider = lom.getBestProvider(criteria, true); lom.requestLocationUpdates(provider, 1000, 10, los); } LocationListener los = new LocationListener(){ public void onLocationChanged(Location location){ if (location != null) { lat = location.getLatitude(); lng = location.getLongitude(); txt_lat.setText("纬度:" + String.valueOf(lat)); txt_lng.setText("经度:" + String.valueOf(lng)); } else{ txt_lat.setText("纬度:未知" ); txt_lng.setText("经度:未知" ); } }; public void onProviderDisabled(String provider){ }; public void onProviderEnabled(String provider){ }; public void onStatusChanged(String provider, int status, Bundle extras){ }; }; // 获取控件 public void get_con(){ txt_time = (TextView) findViewById(R.id.txt_time); txt_lat = (TextView) findViewById(R.id.txt_lat); txt_lng = (TextView) findViewById(R.id.txt_lng); } Handler handler = new Handler(){ public void handleMessage(Message msg){ switch (msg.what){ case 1: get_time(); break; } } }; TimerTask task = new TimerTask(){ public void run() { Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; // 获取时间 public void get_time(){ now = new Date(System.currentTimeMillis()); str_date = formatter.format(now); txt_time.setText("时间:" + str_date); } }
在AndroidManifest.xml檔案中加入權限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
讀者可以在室外訊號充足的地方試試,是可以取得GPS位置的。 希望本文所述對大家的Android程式設計有幫助。
更多Android中GPS定位的用法實例相關文章請關注PHP中文網!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

公司安全軟件導致部分應用無法正常運行的排查與解決方法許多公司為了保障內部網絡安全,會部署安全軟件。 ...

系統對接中的字段映射處理在進行系統對接時,常常會遇到一個棘手的問題:如何將A系統的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架進行數據庫操作時,經常需要根據實體類的屬性名構造查詢條件。如果每次都手動...

將姓名轉換為數字以實現排序的解決方案在許多應用場景中,用戶可能需要在群組中進行排序,尤其是在一個用...

在使用IntelliJIDEAUltimate版本啟動Spring...

Java對象與數組的轉換:深入探討強制類型轉換的風險與正確方法很多Java初學者會遇到將一個對象轉換成數組的�...

電商平台SKU和SPU表設計詳解本文將探討電商平台中SKU和SPU的數據庫設計問題,特別是如何處理用戶自定義銷售屬...

在使用TKMyBatis進行數據庫查詢時,如何優雅地獲取實體類變量名以構建查詢條件,是一個常見的難題。本文將針...
