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"); }}
布局文件如下,
Manifest文件如下,
<?xml version="1.0" encoding="utf-8"?>
输出效果
提示:如果使用模拟器运行这个示例,需要将准确的经纬度发送到模拟器。
如何发送经纬度到模拟器
- 打开Eclipse中 DDMS 视图(
Window>Open Perspective
) - 选择模拟器
- 选择模拟器控制选项
- 在位置控制面板,选择手动输入,添加经纬度数据,点击“发送”
原文 Get Current Location in Android
翻译 伯乐在线 - imesong
更多相关文章
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- python起点网月票榜字体反爬案例
- Android数据库操作总结
- Intent组件
- Android传感器编程实例开发——三轴数据采集
- Android多媒体开发 Pro Android(安卓)Media 第一章 Android图像
- Android常用控件之ListView(一)
- android 连接远程数据库(转)
- Android中结合OrmLite for android组件对SQLite的CRUD(增删改查)