Android编程获取GPS数据的方法详解
作者:pku_android 发布时间:2023-09-20 16:37:34
本文实例讲述了Android编程获取GPS数据的方法。分享给大家供大家参考,具体如下:
GPS是Android系统中重要的组成部分,通过它可以衍生出众多的与位置相关的应用。
Android的GPS有一个专门的管理类,称为LocationManager,所有的GPS定位服务都由其对象产生并进行控制。
首先需要明确的是,LocationManager类的对象获取并不是直接创建的,而是由系统提供的,具体来说,通过如下方法,为一个LocationManager对象建立一个对象引用:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
至此,我们可以用locationManager这个对象对任意有关GPS的功能进行操作了。下表列出了几个常用的成员方法:
方法及其签名 | 描述 |
List<String> getAllProviders() | 获取所有与设备关联的定位模块的列表 |
String getBestProvider(Criteria, boolean) | 获取设定的标准(Criteria对象)中最适合的一个设备 |
GpsStatus getGpsStatus(GpsStatus) | 获取GPS当前状态 |
Location getLastKnownLocation(String) | 获取最近一次的可用地点信息 |
boolean isProviderEnabled(String) | 判断参数所提及的设备是否可用 |
GPS还有一个支持API,即Location,它的作用是一个代表位置信息的抽象类,用它可以获取所有的位置数据:
方法及其签名 | 描述 |
double getAltitude() | 获取当前高度 |
float getBearing() | 获取当前方向 |
double getLatitude() | 获取当前纬度 |
double getLongitude() | 获取当前经度 |
float getSpeed() | 获取当前速度 |
我们可以用以上的方法开始进行定位。
可以将地点信息传递给一个Location对象:
Locationlocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
我们还可以调用以下函数,对每次更新的位置信息进行我们想要的操作:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 10, new LocationListener())
其中,第一个参数是LocationProvider对象,第二个参数是刷新的时间差,这里设定为1秒,第三个参数是位置差,这里设定为10米,第四个参数为一个位置 * 对象,它必须实现4个方法:
①. public void onLocationChanged(Location location)
②. public void onProviderDisabled(String provider)
③. public void onProviderEnabled(String provider)
④. public void onStatusChanged(String provider, int status, Bundleextras)
可以重写这些方法来实现我们的需求。
当我们使用模拟器进行测试的时候,由于模拟器无法获取地理位置,所以必须用Emulator的位置控制器进行设置:
最终的结果如图所示:
代码如下所示:
package org.timm.android;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText;
public class LocationTryActivity extends Activity {
EditText text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
text = (EditText)findViewById(R.id.textShow);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
showLocation(location);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new LocationListener(){
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
showLocation(location);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
showLocation(null);
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
showLocation(locationManager.getLastKnownLocation(provider));
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
});
}
public void showLocation(Location currentLocation){
if(currentLocation != null){
String s = "";
s += " Current Location: (";
s += currentLocation.getLongitude();
s += ",";
s += currentLocation.getLatitude();
s += ")\n Speed: ";
s += currentLocation.getSpeed();
s += "\n Direction: ";
s += currentLocation.getBearing();
text.setText(s);
}
else{
text.setText("");
}
}
}
最后一点需要说明的是,需要在AndroidManifest.xml中设置许可:
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" />
PS:关于AndroidManifest.xml详细内容可参考本站在线工具:
Android Manifest功能与权限描述大全:
http://tools.jb51.net/table/AndroidManifest
希望本文所述对大家Android程序设计有所帮助。


猜你喜欢
- 通常我们用惯的ListView每一项的布局都是相同的,只是控件所绑定的数据不同。但单单只是如此并不能满
- 为大家提供的MySQL忘记密码的解决方案,供大家参考,具体内容如下1.在操作系统windows操作系统,xp或win7.中进入如下目录:C:
- 在项目迁移到Spring Boot之后,发生内存使用量过高的问题。本文介绍了整个排查过程以及使用到的工具,也非常适用于其他堆外内存排查。背景
- 本文以实例介绍了C#如何通过winmm.dll来播放声音,主要实现步骤如下:1.首先导入如下两个函数:/// <summary>
- 引言使用SpringMVC作为Controller层进行Web开发时,经常会需要对Controller中的方法进行参数检查。本来Spring
- 本文实例讲述了Android编程使用AlarmManager设置闹钟的方法。分享给大家供大家参考,具体如下:package com.Aina
- 本文实例为大家分享了C#实现银行家算法的具体代码,供大家参考,具体内容如下1.死锁死锁,顾名思义,是一种锁住不可自行解开的死局。在操作系统中
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 为何要使用同步? java允许多线程并发控制,当多个线程同时操作一个可共享的资源变量时(如数
- 本文为大家分享了经典24点纸牌益智游戏的具体实现方法,供大家参考,具体内容如下一.实验内容24点游戏是经典的纸牌益智游戏。常见游戏规则:从扑
- 本文实例讲述了C#动态创建button的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.C
- 官方文档 https://developer.android.google.cn/guide/components/activit
- 本文实例为大家分享了android实现简单时钟的具体代码,供大家参考,具体内容如下attrs定义如下<?xml version=&qu
- 首先来看一下效果: 大体思路如下: 总体布局用了一个自定义的ViewGroup,里面包了两个View(top Vie
- 本篇博客要分享的一个UI效果——实现底部切换标签,想必大家在一些应用上面遇到过这种效果了,最典型的就是微信了,可以左右滑动切换页面,也可以点
- 在我们对程序进行操作过程中,一般都需要有一个操作流程的记录显示。用C#进行编程时可以很容易实现这个功能。本经验提供案例仅供参考下面小编就来介
- 任意位置获取HttpServletRequest对象方法一//获取RequestAttributes RequestAttributes r
- 一、什么是抽象工厂模式为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类。抽象工厂模式是所有形态的工厂模式中最为抽象和最具
- 1.更新MAC地址 将注册表中的键值添加上MAC地址2.重新连接网络 试过了3个方法: Ma
- redissonredisson 实现分布式锁的机制如下:依赖版本implementation 'org.redisson:redi