天天看点

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

  • 选择模拟器
  • 选择模拟器控制选项
  • 在位置控制面板,选择手动输入,添加经纬度数据,点击“发送”