android自动获取位置,Android中获取当前位置信息

这篇教程主要介绍了在Android平台上如何使用服务完成定位功能。众所周知,Android设备的当前位置信息,对开发创新性App、解决人们日常生活问题有极大帮助。在Android平台开发定位相关的应用程序,需要位置提供者。有两种类型的位置提供者:

GPS定位

网络定位

以上两种类型,任何一种都可以获得用户或者用户设备的位置信息。但是,它们各有优劣,推荐两者同时使用。GPS 定位,在室内反应迟缓,比较耗时;网络定位,在没有网络的时候无法获得位置信息。

GPS定位 VS 网络定位

获取位置坐标时,网络定位比GPS定位稍快。

GPS在室内定位非常缓慢,并且比较耗电。

网络定位依赖蜂窝网络,获取的是最近的网络基站的位置。

GPS定位数据相对精确,得到我们当前的位置信息。

获取定位数据

在Manifest文件中授权,接收定位数据。

创建LocationManager实例,将其指向定位服务。

LocationManager请求定位数据。

定数数据改变时,LocationListener接收更新的定位数据。

授权接收定位更新数据

在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);

}

@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");

}

}

布局文件如下,

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

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

Manifest文件如下,

package="com.javapapers.android.geolocationfinder"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="8"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppBaseTheme" >

android:name="com.javapapers.android.geolocationfinder.MainActivity"

android:label="@string/app_name" >

输出效果

bVb3Vl

提示:如果使用模拟器运行这个示例,需要将准确的经纬度发送到模拟器。

如何发送经纬度到模拟器

打开Eclipse中 DDMS 视图(Window>Open Perspective)

选择模拟器

选择模拟器控制选项

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值