Android中獲取當前位置信息

這篇教程主要介紹了在Android平臺上如何使用服務完成定位功能。衆所周知,Android設備的當前位置信息,對開發創新性App、解決人們平常生活問題有極大幫助。在Android平臺開發定位相關的應用程序,須要位置提供者。有兩種類型的位置提供者:java

  1. GPS定位
  2. 網絡定位

以上兩種類型,任何一種均可以得到用戶或者用戶設備的位置信息。可是,它們各有優劣,推薦二者同時使用。GPS 定位,在室內反應遲緩,比較耗時;網絡定位,在沒有網絡的時候沒法得到位置信息。android

GPS定位 VS 網絡定位

  • 獲取位置座標時,網絡定位比GPS定位稍快。
  • GPS在室內定位很是緩慢,而且比較耗電。
  • 網絡定位依賴蜂窩網絡,獲取的是最近的網絡基站的位置。
  • GPS定位數據相對精確,獲得咱們當前的位置信息。

獲取定位數據

  1. Manifest文件中受權,接收定位數據。
  2. 建立LocationManager實例,將其指向定位服務。
  3. LocationManager請求定位數據。
  4. 定數數據改變時,LocationListener接收更新的定位數據。

受權接收定位更新數據

Manifest文件中獲取以下權限,而後能夠經過定位提供者得到定位數據:git

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

定位提供者必須要有INTERNET權限和ACCESS_FINE_LOCATION權限。同時,網絡定位還須要ACCESS_COARSE _LOCATION權限。segmentfault

建立LocationManager實例,指向定位服務

不管何種類型的Android後臺Service,須要得到其引用才能使用。一樣,經過getSystemService()方法得到定位服務的引用,而後將這個引用將添加到新建立的LocationManager實例中,示例以下:網絡

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

從LocationManager請求當前位置

穿件位置服務引用後,經過LocationManagerrequestLocationUpdates()方法能夠請求位置更新信息。調用方法時,須要位置提供者、最後一次更新距今的時間(秒)、距離和LocationListener對象。調用後LocationListener對象會根據位置進行更新。app

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

經過LocationListener,得到更新位置數據

根據指定的距離或時間間隔,LocationListener會收到更新通知。ide

示例:獲取當前位置

這個示例經過GPS定位獲取當前位置數據。主要代碼以下:佈局

package com.javapapers.android.geolocationfinder;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;

import android.util.Log;

public class MainActivity extends Activity implements LocationListener {
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
    TextView txtLat;
    String lat;
    String provider;
    protected String latitude, longitude;
    protected boolean gps_enabled, network_enabled;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtLat = (TextView) findViewById(R.id.textview1);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        txtLat = (TextView) findViewById(R.id.textview1);
        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"
                + location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
        Log.d("Latitude", "disable");
    }

    @Override
    public void onProviderEnabled(String provider) {
        Log.d("Latitude", "enable");
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude", "status");
    }
}

佈局文件以下,this

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

Manifest文件以下,spa

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.javapapers.android.geolocationfinder"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17"        />
    <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="@style/AppBaseTheme" >
        <activity
            android:name="com.javapapers.android.geolocationfinder.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>

輸出效果

提示:若是使用模擬器運行這個示例,須要將準確的經緯度發送到模擬器。

如何發送經緯度到模擬器

  • 打開Eclipse中 DDMS 視圖(Window>Open Perspective
  • 選擇模擬器
  • 選擇模擬器控制選項
  • 在位置控制面板,選擇手動輸入,添加經緯度數據,點擊「發送」

原文 Get Current Location in Android
翻譯 伯樂在線 - imesong

相關文章
相關標籤/搜索