18、GPS技术
Android SDK为GPS提供了很多API,其中LocationManager类是这些API的核心。LocationManager是一个系统服务类,与TelephonyManager、AudioManager等服务类的作用和创建服务类对象的方法类似。所有与GPS相关的操作都由LocationManager对象及其派生的对象完成。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationProvider类可以获取与位置提供者相关的信息。
Location类是对具体位置信息的抽象表示。
使用GPS定位的关键之一就是获取LocationProvider,每一个LocationProvider对象都表示一个抽象的定位系统。无论使用GPS做什么,都需要首先获取合适的LocationProvider对象。
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; /**
* 得到 所有的LocationProvider。
* @author dr
*
*/
public class AllProvidersActivity extends Activity {
ListView mProviders;
LocationManager mLocationManager; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mProviders = (ListView) findViewById(R.id.providers); // 首先应获取LocationManager对象
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 获取所有的LocationProvder的名称
List<String> providerNames = mLocationManager.getAllProviders(); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, providerNames); mProviders.setAdapter(adapter);
}
}
根据名称获取LocationProvider
// 获取passive Location Provider
LocationProvider passiveProvider =
mLocationManager.getProvider(LocationManager.PASSIVE_PROVIDER);
// 获取gps Location Provider
LocationProvider gpsProvider =
mLocationManager.getProvider(LocationManager.GPS_PROVIDER);
// 获取network Location Provider
LocationProvider passiveProvider =
mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER);

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 LocationActivity extends Activity {
LocationManager mLocationManager;
EditText mEditText; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mEditText = (EditText) findViewById(R.id.show); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // 从GPS获取最近的定位信息
Location location = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER); updateView(location); // 2000(2秒), 8(米)。
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
2000, 8, new LocationListener() { @Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
} @Override
public void onProviderEnabled(String provider) {
// 当GPS Location Provider可用时,更新位置
updateView(mLocationManager
.getLastKnownLocation(provider));
} @Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
} @Override // 位置发生变化。
public void onLocationChanged(Location location) {
// 当GPS定位信息发生改变时,更新位置
updateView(location);
}
}); } // 用来更新EditText中的信息
public void updateView(Location newLocation) {
if (newLocation != null) {
StringBuilder sb = new StringBuilder();
sb.append("实时的位置信息:\n");
sb.append("经度:");
sb.append(newLocation.getLongitude());
sb.append("\n纬度:");
sb.append(newLocation.getLatitude());
sb.append("\b高度:");
sb.append(newLocation.getAltitude());
sb.append("\n速度:");
sb.append(newLocation.getSpeed());
sb.append("\n方向:");
sb.append(newLocation.getBearing());
mEditText.setText(sb.toString()); }
}
}
<!-- 授权获取定位信息 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

mEditText = (EditText) findViewById(R.id.edittext);
final float[] results = new float[3]; Location.distanceBetween(20.123, 30.05644, 30.124, 40.543, results);
mEditText.setText(String.valueOf(results[0]) + "米");

import mobile.android.proximity.alert.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle; public class ProximityAlertActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); double longitude = 123.427109;
double latitude = 41.764998;
float radius = 2000; //距离中心点半径,单位:米。 Intent intent = new Intent(this, PromimityAlertRecivery.class); // 任何情况下都可以发intent,除非关机。intent的话如果关闭当前activity或者程序,将终止。
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1,
intent, 0); locationManager.addProximityAlert(latitude, longitude, radius, -1,
pendingIntent);
}
}
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.widget.Toast; /**
* 广播接收器
* @author dr
*/
public class PromimityAlertRecivery {
public void onReceive(Context context, Intent intent) {
// 获取是否进入指定区域
boolean isEnter = intent.getBooleanExtra(
LocationManager.KEY_PROXIMITY_ENTERING, false);
if (isEnter) {
Toast.makeText(context, "您已经进入指定区域", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "您已经离开指定区域", Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mobile.android.proximity.alert"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="cn.proximity.alert.ProximityAlertActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="cn.proximity.alert.ProximityAlertReciever" />
</application>
<!-- 授权获取定位信息 -->
<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>
18、GPS技术的更多相关文章
- GPS
百度百科 http://baike.baidu.com/link?url=Kl6eLdP-fveCsHt1wHF8TVuOR9wkT2K3qFnWy36PcaYaB1hdgOS_cnTEB0jIg ...
- 趣味GPS
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 简介 GPS的全称是全球定位系统(the Global Positioning S ...
- 【转】GPS连续运行单参考站解决方案
GPS连续运行单参考站解决方案 一. 前言 随着国家信息化程度的提高及计算机网络和通信技术的飞速发展,电子政务.电子商务.数字城市.数字省区和数字地球的工程化和现实化,需要采集多种实时地理 空间 ...
- [IC]Lithograph(1)光刻技术分析与展望
文章主体转载自: 1.zol摩尔定律全靠它 CPU光刻技术分析与展望 2.wiki:Extreme ultraviolet lithography 3.ITRS 2012 1. 光刻技术组成和关键点 ...
- 和菜鸟一起学android4.0.3源码之硬件gps简单移植【转】
本文转载自:http://blog.csdn.net/mwj19890829/article/details/18751447 关于Android定位方式 android 定位一般有四种方法,这四种方 ...
- 浅谈iOS需要掌握的技术点
鉴于很多人的简历中的技术点体现(很多朋友问我iOS需要知道注意哪些)! 技术点: 1.热更新 (及时解决线上问题) 2.runtime(json解析.数据越界.扩大button点击事件.拦截系统方法) ...
- GPS常识-B版(简)
第一章 绪论 1.简述GPS系统的特点有哪些? 在测绘工程中有如下优点:(1)定位精度高(2)观测时间短(3)测站间无需通视(4)可提供地心坐标(5)操作简便(6)全天候作业(7)功能多.应用广 GP ...
- GPS常识-A版(详)
第一章 绪论 1.简述GPS系统的特点有哪些? GPS在测绘工程中应用的优点 P13 ●定位精度高 应用实践证明,相对静态定位1小时以上观测解,其平面位置:在300-1500m范围内,绝对误差小于1m ...
- 窥见云技术未来大势,腾讯云Techo开发者大会即将在京召开
云.物联网.5G.人工智能……一项项技术的突破带来了天翻地覆的变化,开发者们是如何一次次地进行天马行空的创意和极限突破?2019年11月6日-7日,由腾讯云主办的首届Techo开发者大会将在北京嘉里大 ...
随机推荐
- Struts2笔记——初次框架配置
1.Struts2简介 Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与S ...
- 309. Best Time to Buy and Sell Stock with Cooldown
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- Linux任务前后台的切换
Shell支持作用控制,有以下命令实现前后台切换: 1. command& 让进程在后台运行 2. jobs 查看后台运行的进程 3. fg %n 让后台运行的进程n到前台来 4. bg %n ...
- Database File Management ->> Shrink Data File
今天在开发环境遇到了一个问题,我们发现服务器上的硬盘空间满了,查看了下发现这个盘存放的数据库文件应该是来源一个并非很大的库才对.检查之后发现这个数据库下的某个数据文件占了盘符下70%的空间,而大部分数 ...
- Git工作流指南:Gitflow工作流 Comparing Workflows
Comparing Workflows The array of possible workflows can make it hard to know where to begin when imp ...
- 如何写计算机会议的rebuttal
其实最好的教材就是实例,恰好NIPS会议会把往年所有论文的Rebuttal都贴出来...,见这里:http://papers.nips.cc/ 同时,圈内同行也总结了不少经验,下面转帖其他人的经验 = ...
- 【原创】通俗易懂地解决中文乱码问题(2) --- 分析解决Mysql插入移动端表情符报错 ‘incorrect string value: '\xF0...
这篇blog重点在解决问题,如果你对字符编码并不是特别了解,建议先看看 < [原创]通俗易懂地解决中文乱码问题(1) --- 跨平台乱码 >. 当然,如果只是针对解决这个Mysql插入报错 ...
- jquery 计算输入的文本的utf-8字节长度
jquery-2.1.1.min.js /*! jQuery v2.1.1 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license ...
- PHP高级特性二之文件处理
PHP中的文件处理也是一个相当重要的模块,这一篇的主要内容就是PHP中文件系统的简介. 文件系统用途 1. 项目处理都离不开文件处理 2. 可以用文件长时间保存数据 3. 建立缓存,在服务器中进行文件 ...
- Json工具类 - JsonUtils.java
Json工具类,提供Json与对象之间的转换. 源码如下:(点击下载 - JsonUtils.java . gson-2.2.4.jar ) import java.lang.reflect.Type ...