Android开发--用户定位服务--UserLocation
Android开发--用户定位服务--UserLocation
2013-01-28 08:32:26 我来说两句 作者:BruceZhang
收藏 我要投稿
[java]
<span style="background-color: rgb(243, 248, 251); font-family: simsun;">用户定位介绍:</span>
User Location的作用:
1.获取用户的位置
2.追踪用户的移动
User Location的关键API
1.Location Manager:用于管理Android的用户定位服务
2.Location Providers:提供多种定位方式供开发者选择。
<1>GPS Provider
<2>Network Provider
<3>Passive
定位方式的分类:
1.GPS定位:使用GPS卫星进行定位,需要在AndroidManifest.xml当中声明如下权限:
android.permission.ACCESS_FINE_LOCATION
2.NETWORK定位:使用信号接收塔和WIFI接入点进行定位,需要在AndroidManifest.xml当中声明如下权限:
android.permission.ACCESS_FINE_LOCATION
或
android.permission.ACCESS_COARSE_LOCATION
以上两种定位方式的区别是GPS定位精度更高,但同时也更耗电
获取用户的当前位置:
1.在AndroidManifest.xml当中声明相应的权限;
2.获取LocationManager对象;
3.选择LocationProvider;
4.绑定LocationListener对象。
LocationListener有四个方法:
1.onLocationChanged(Location location):当设备的位置发生改变时调用
我们可以调用location.getLongitude()和location.getLatitude()来得到设备所处的经度和纬度
2.onProviderDisabled(String provider):当提供数据Provider禁用时调用
3.onProviderEnabled(String provider):当提供数据的Provider使用时调用
4.onStatusChanged(String provider,int status,Bundle extras):当状态改变时
我们需要实现LocationListener的以上四个方法:
[java]
<span style="font-size:18px;">private class TestLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location){
System.out.println(location.getLongitude());
System.out.println(location.getLatitude());
}
@Override
public void onProviderDisabled(String provider){
// do something you need
}
@Override
public void onProviderEnabled(String provider){
// do something you need
}
@Override
public void onStatusChanged(String provider,int status,Bundle extras){
// do something you need
}
}</span>
测试当前设备的LocationProvider
由于一般的设备存在不止一种定位方法,所以在这里给出查找定位服务的方法:
[java]
<span style="font-size:18px;">List<String> providers=locationManager.getAllProviders();
for(Iterator<String> iterator=providers.iterator();iterator.hasNext();){
String string=(String)iterator.next();
Log.d("BruceZhang", string+"\n");</span>
由于有多个Provider,那么就需要做出选择,在这里给出选择最好的Provider的方法:
此时需要用到一个类--Criteria
下面是在Android SDK文档上给出的解释:
A class indicating the application criteria for selecting a location provider. Providers maybe ordered according to accuracy, power usage, ability to report altitude, speed, and bearing, and monetary cost.
它提供了一系列的方法,设置用户的需求,并最终给出用户所需要的最佳的Provider,下面是文档上对设置条件的解释:
[java] view plaincopy
<span style="font-size:18px;">void setAccuracy(int accuracy)
Indicates the desired accuracy for latitude and longitude.</span>
[java]
<span style="font-size:18px;">
void setAltitudeRequired(boolean altitudeRequired)
Indicates whether the provider must provide altitude information.</span>
[java]
<span style="font-size:18px;">
void setBearingAccuracy(int accuracy)
Indicates the desired bearing accuracy.</span>
[java
<span style="font-size:18px;">
void setBearingRequired(boolean bearingRequired)
Indicates whether the provider must provide bearing information.</span>
[java]
<span style="font-size:18px;">
void setCostAllowed(boolean costAllowed)
Indicates whether the provider is allowed to incur monetary cost.</span>
[java]
<span style="font-size:18px;">
void setHorizontalAccuracy(int accuracy)
Indicates the desired horizontal accuracy (latitude and longitude).</span>
[java]
<span style="font-size:18px;">
void setPowerRequirement(int level)
Indicates the desired maximum power level.</span>
[java]
<span style="font-size:18px;">
void setSpeedAccuracy(int accuracy)
Indicates the desired speed accuracy.</span>
[java] view plaincopy
<span style="font-size:18px;">
void setSpeedRequired(boolean speedRequired)
Indicates whether the provider must provide speed information.</span>
[java] view plaincopy
<span style="font-size:18px;">
void setVerticalAccuracy(int accuracy)
Indicates the desired vertical accuracy (altitude).</span>
追踪用户的位置:
对用户的位置进行更新用到的方法和解释如下:
[java]
// public void requestLocationUpdates (String provider,
// long minTime, float minDistance, LocationListener listener)
// Added in API level 1
// Register for location updates using the named provider, and a pending intent.
//
// Parameters
// provider the name of the provider with which to register
// minTime minimum time interval between location updates, in milliseconds
// minDistance minimum distance between location updates, in meters
// listener a LocationListener whose onLocationChanged(Location) method will be called for each location update
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new TestLocationListener());
下面,给出一个例子,实现用户的定位,取得支持的LocationProvider,根据条件获取最佳的Provider:
一下是实现的源代码:
[java]
public class MainActivity extends Activity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button4=(Button)findViewById(R.id.button4);
button.setOnClickListener(new ButtonListener());
button2.setOnClickListener(new ProviderButtonListener());
button3.setOnClickListener(new BestProviderButtonListener());
button4.setOnClickListener(new MyLocation());
locationManager=(LocationManager)MainActivity.this.
getSystemService(Context.LOCATION_SERVICE);
}
private class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// LocationManager locationManager=(LocationManager)MainActivity.this.
// getSystemService(Context.LOCATION_SERVICE);
/*
* 各个参数的意义:
* 1.定义当前所使用的Location Provider
* 2.位置更新一次的最小时间间隔
* 3.位置更新的最小距离
* 4.绑定监听器--位置发生变化会调用其中的方法
*/
Log.d("BruceZhang", "Bond Success");
// public void requestLocationUpdates (String provider,
// long minTime, float minDistance, LocationListener listener)
// Added in API level 1
// Register for location updates using the named provider, and a pending intent.
//
// Parameters
// provider the name of the provider with which to register
// minTime minimum time interval between location updates, in milliseconds
// minDistance minimum distance between location updates, in meters
// listener a LocationListener whose onLocationChanged(Location) method will be called for each location update
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new TestLocationListener());
}
}
private class ProviderButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<String> providers=locationManager.getAllProviders();
for(Iterator<String> iterator=providers.iterator();iterator.hasNext();){
String string=(String)iterator.next();
Log.d("BruceZhang", string+"\n");
}
}
}
private class BestProviderButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Criteria criteria=new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(false);
//第二个参数设置为false时,不管当前的那个provider是否可用,都需要进行查找,并根据条件设为最优
String provider=locationManager.getBestProvider(criteria, false);
Log.d("BruceZhang", "The best provider is:"+provider);
}
}
private class MyLocation implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//对用用户定位服务主要是中间两个参数的设置
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000,
2000, new TestLocationListener());
}
}
private class TestLocationListener implements LocationListener{
//这个函数的参数是用户当前的位置
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
// Toast.makeText(MainActivity.this, "您当前的经度是:"+arg0.getLongitude()+" ,"+
// "您当前的纬度是:"+arg0.getLatitude(),
// Toast.LENGTH_SHORT).show();
Log.d("BruceZhang", arg0.getLongitude()+"");
Log.d("BruceZhang", arg0.getLatitude()+"");
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override www.2cto.com
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Android开发--用户定位服务--UserLocation的更多相关文章
- Android开发-API指南-服务
Service 英文原文:http://developer.android.com/guide/components/services.html 采集(更新)日期:2014-12-23 原博客:htt ...
- [android学习]android_gps定位服务简单实现
前言 gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在照着案列敲了两遍之后,发现老师的案例是在是太老了,并且直接 ...
- Android开发——用户在屏幕上的手势识别
个定点决定.四个属性分别为left(1),top(2),right(3),bottom(4). 数字为图上标出的距离.显然这四个属性是相对于父容器来定的,均可以通过get()方法获取. 因此很容易得出 ...
- Android开发学习—— Service 服务
Service运行于后台的一个组件,用来运行适合运行在后台的代码,服务是没有前台界面,可以视为没有界面的activity. 服务可以被手动关闭,不会重启,但是如果被自动关闭,内存充足就会重启. sta ...
- android开发——用户头像
最近,小灵狐得知了一种能够加快修炼速度的绝世秘法,那便是修炼android神功.小灵狐打算用android神功做一个app,今天他的修炼内容就是头像功能.可是小灵狐是个android小白啊,所以修炼过 ...
- Android开发——GPS定位
1.LocationManager LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题. 与LocationManager相关的两个知识点: 1.1 ...
- IOS 定位服务与地图的应用开发
1.定位服务 现在的移动设备很多都提供定位服务,IOS设备提供3种不同定位途径: (1)WiFi定位,通过查询一个WiFi路由器的地理位置的信息,比较省电:IPhone,IPod touch和IPad ...
- iOS开发拓展篇—CoreLocation定位服务
iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...
- Android网络定位服务定制简述
Android 添加高德或百度网络定位服务 Android的网络定位服务以第三方的APK方式提供服务,由于在国内Android原生自带的com.google.android.gms服务几乎处于不可用状 ...
随机推荐
- 四十九 常用内建模块 urllib
urllib提供了一系列用于操作URL的功能. Get urllib的request模块可以非常方便地抓取URL内容,也就是发送一个GET请求到指定的页面,然后返回HTTP的响应: 例如,对豆瓣的一个 ...
- python的vim的环境配置(后续慢慢补充进来)
#vim ~/.vimrc set smartindent set smarttab set expandtab set tabstop= set softtabshtop= set shiftwid ...
- PAT L3-011. 直捣黄龙
最短路. 先求出一个只包含最短路的$DAG$,然后在$DAG$上$dp$一下就可以了.英文变数字还有$map$转一下. #include<map> #include<set> ...
- ZOJ 3206 Disaster Area Reconstruction
已经确定本题是错题了! ---------------------------------------------------------------- 这题有问题,不是后台数据水了就是出题人的做法是 ...
- 拜拜了,GOPATH君!新版本Golang的包管理入门教程
Go 1.11和1.12实现了对包管理的初步支持,Go的新依赖管理系统使依赖版本信息明确且易于管理.Using Go Modules - The Go Blog 新的包管理模式有什么不同? 作为Go语 ...
- webpack HMR原理
在启动 devServer 的时候,sockjs 在服务端和浏览器端建立了一个 webSocket 长连接,以便将 webpack 编译和打包的各个阶段状态告知浏览器,最关键的步骤还是 webpack ...
- 洛谷——P1177 【模板】快速排序
P1177 [模板]快速排序. 题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成. ...
- Python开发基础-Day8-装饰器扩展和迭代器
wraps模块 让原函数保留原来的说明信息 import time import random from functools import wraps def auth(func): '''auth ...
- python正则表达式中的分组 group
维基百科:http://wiki.ubuntu.org.cn/Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E6%93%8D%E4%BD%9C ...
- BZOJ 2084 [Poi2010]Antisymmetry(manacher)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2084 [题目大意] 对于一个01字符串,如果将这个字符串0和1取反后, 再将整个串反过 ...