天天看點

Android中擷取目前位置資訊

這篇教程主要介紹了在Android平台上如何使用服務完成定位功能。衆所周知,Android裝置的目前位置資訊,對開發創新性App、解決人們日常生活問題有極大幫助。在Android平台開發定位相關的應用程式,需要位置提供者。有兩種類型的位置提供者:

  1. GPS定位
  2. 網絡定位

以上兩種類型,任何一種都可以獲得使用者或者使用者裝置的位置資訊。但是,它們各有優劣,推薦兩者同時使用。GPS 定位,在室内反應遲緩,比較耗時;網絡定位,在沒有網絡的時候無法獲得位置資訊。

GPS定位 VS 網絡定位

  • 擷取位置坐标時,網絡定位比GPS定位稍快。
  • GPS在室内定位非常緩慢,并且比較耗電。
  • 網絡定位依賴蜂窩網絡,擷取的是最近的網絡基站的位置。
  • GPS定位資料相對精确,得到我們目前的位置資訊。

擷取定位資料

  1. Manifest

    檔案中授權,接收定位資料。
  2. 建立

    LocationManager

    執行個體,将其指向定位服務。
  3. LocationManager

    請求定位資料。
  4. 定數資料改變時,

    LocationListener

    接收更新的定位資料。

授權接收定位更新資料

Manifest

檔案中擷取如下權限,然後可以通過定位提供者獲得定位資料:

<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

權限。

建立LocationManager執行個體,指向定位服務

無論何種類型的

Android背景Service

,需要獲得其引用才能使用。同樣,通過

getSystemService()

方法獲得定位服務的引用,然後将這個引用将添加到新建立的

LocationManager

執行個體中,示例如下:

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

從LocationManager請求目前位置

穿件位置服務引用後,通過

LocationManager

requestLocationUpdates()

方法可以請求位置更新資訊。調用方法時,需要位置提供者、最後一次更新距今的時間(秒)、距離和

LocationListener

對象。調用後

LocationListener

對象會根據位置進行更新。

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

通過LocationListener,獲得更新位置資料

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

示例:擷取目前位置

這個示例通過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);

    }

    public void onLocationChanged(Location location) {

        txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:"

                + location.getLongitude());

    public void onProviderDisabled(String provider) {

        Log.d("Latitude", "disable");

    public void onProviderEnabled(String provider) {

        Log.d("Latitude", "enable");

    public void onStatusChanged(String provider, int status, Bundle extras) {

        Log.d("Latitude", "status");

}

布局檔案如下,

<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檔案如下,

<?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"        />

    <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>

輸出效果

Android中擷取目前位置資訊

提示:如果使用模拟器運作這個示例,需要将準确的經緯度發送到模拟器。

如何發送經緯度到模拟器

  • 打開Eclipse中 DDMS 視圖(

    Window>Open Perspective

  • 選擇模拟器
  • 選擇模拟器控制選項
  • 在位置控制台,選擇手動輸入,添加經緯度資料,點選“發送”