Android GPS擷取當前經緯度座標

來源:互聯網
上載者:User

標籤:android gps   android 地圖開發   android 百度地圖開發   android天地圖開發   android 定位   

APP中可能會遇到一種需求,就是將當前所在位置的座標傳到伺服器上,今天我提供三種途徑去擷取經緯度座標資訊,第一種是通過Android API來實現,第二種通過百度地圖API來實現,第三種通過天地圖API來實現。

第一種方法(Android API實現),廢話不多說,上代碼。

MainActivity代碼如下:

public class MainActivity extends Activity {private static final String TAG = MainActivity.class.getSimpleName();private double latitude = 0.0;private double longitude = 0.0;private TextView info;private LocationManager locationManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);info = (TextView) findViewById(R.id.tv);locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {getLocation();//gps已開啟} else {toggleGPS();new Handler() {}.postDelayed(new Runnable() {@Overridepublic void run() {getLocation();}}, 2000);}}private void toggleGPS() {Intent gpsIntent = new Intent();gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");gpsIntent.addCategory("android.intent.category.ALTERNATIVE");gpsIntent.setData(Uri.parse("custom:3"));try {PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();} catch (CanceledException e) {e.printStackTrace();locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);if (location1 != null) {latitude = location1.getLatitude(); // 經度longitude = location1.getLongitude(); // 緯度}}}private void getLocation() {Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (location != null) {latitude = location.getLatitude();longitude = location.getLongitude();} else {locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);}info.setText("緯度:" + latitude + "\n" + "經度:" + longitude);}LocationListener locationListener = new LocationListener() {// Provider的狀態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}// Provider被enable時觸發此函數,比如GPS被開啟@Overridepublic void onProviderEnabled(String provider) {Log.e(TAG, provider);}// Provider被disable時觸發此函數,比如GPS被關閉@Overridepublic void onProviderDisabled(String provider) {Log.e(TAG, provider);}// 當座標改變時觸發此函數,如果Provider傳進相同的座標,它就不會被觸發@Overridepublic void onLocationChanged(Location location) {if (location != null) {Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());latitude = location.getLatitude(); // 經度longitude = location.getLongitude(); // 緯度}}};/* *  * 開啟和關閉gps第二種方法 * private void openGPSSettings() {//擷取GPS現在的狀態(開啟或是關閉狀態)boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);if (gpsEnabled) {//關閉GPSSettings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);} else {//開啟GPS  Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);}}*/}
main.xml布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white"    android:orientation="vertical" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="經緯度資訊:"        android:textColor="#660000"        android:textSize="20sp" /></LinearLayout>
資訊清單檔如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.tqmapdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />      <!-- 串連互連網Internet許可權 -->    <uses-permission android:name="android.permission.INTERNET" />    <!-- GPS定位許可權 -->    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />    <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="@android:style/Theme.Black" >        <activity            android:name="com.example.tqmapdemo.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>
運行結果如下

下載Demo請猛戳

第二種方法(百度地圖API實現,註:需要自己申請apikey)

下載Demo請猛戳


第三種方法(天地圖API實現)

下載Demo請猛戳



Android GPS擷取當前經緯度座標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.