Android开发之定位系统
2013-07-04
定位系统
全球定位系统(Global Positioning System, GPS), 又称全球卫星定位系统。
最少只需其中3颗卫星,就能迅速确定用户组地球所处的位置及海拔高度,所能连接的卫星数越多,解析出来的位置就越精确。
广泛应用于军事、物流、地理、移动电话、数码相机、航空等领域。
包android.location提供地理位置API ,其中几个重要的类:
LocationManager, 提供访问定位服务,获取最佳定位提供者,临近报警等功能。
LocationProvider, 具备周期性报告设备地理位置的功能。
LocationListener, 提供定位信息发送变化时回调功能。
Criteria, 通过LocationProvider中设置的属性来选择合适的定位提供者。
Geocode, 处理地理编码(将地址或其他描述转变为经度和纬度)和反向地理编码(将经度和纬度转变为地址或描述)。
生词:
Criteria 标准,尺度,准则
// 得到LocationManager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// 注册LocationListener
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
// LocationListener中的几个抽象方法
// 当坐标发生变化时调用
onLocationChanged(Location location)
// 当LocationProvider禁用时调用
onProviderDisabled(String provider)
// 当LocationProvider启用时调用
onProviderEnabled(String provider)
// 当LocationProvider的状态发生变化时调用
onStatusChanged(String provider, int status, Bundle extras)
// 在AndroidManifest.xml文件中添加权限
<uses-permission android:name=”android.permission.INTERNET” />
<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
// 在模拟器上设置坐标
Eclipse, Window->Show View->Emulator Control, 手动或通过KML和GPX文件来设置一个坐标。
// 使用geo命令
telnet到本机的5554端口,在命令行下输入geo fix-121.45354 46.5119 4392
后面3个参数分别代表经度,纬度,和海拔
示例:
public class LocationActivity extends MapActivity {
private LocationManager locationManager;
private MapView mapView;
private MapController mapController;
private GeoPoint geoPoint;
private static final int ZOOM_IN = Menu.FIRST;
private static final int ZOOM_OUT = Menu.FIRST + 1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mapView = (MapView) findViewById(R.id.mapView01);
mapView.setTraffic(true);
mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.displayZoomControls(false);
mapView.setEnabled(true);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
geoPoint = new GeoPoint((int)(30.659259*1000000), (int)(104.065762*1000000));
mapController.animateTo(geoPoint);
mapController.setZoom(17);
LocationOverlay locationOverlay = new LocationOverlay();
List<Overlay> list = mapView.getOverlays();
list.add(locationOverlay);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
// 得到最近位置
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
// 注册周期性更新,每隔3秒更新一次
locationManager.requestLocationUpdates(provider, 3000, 0, locationListener);
}
private void updateWithNewLocation(Location location) {
String latLng = “”; // 经纬度
TextView textView = (TextView) findViewById(R.id.textview01);
String addressInfo = “没有找到地址\n”;
if(location == null) {
latLng = addressInfo;
} else {
locationOverlay.setLocation(location);
// 得到经度和纬度
Double geoLat = location.getLatitude() * 1E6;
Double geoLng = location.getLongitude() * 1E6;
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());
// 在地图上定位到指定位置
mapController.animateTo(point);
double lat = location.getLatitude();
double lng = location.getLongitude();
latLng = “经度:”+lat+”,纬度:”+lng;
// 设置本地编码方式
Geocode geocode = new Geocode(this, Locale.getDefault());
try {
List<Address> addresses = geocode.getFromLocation(lat, lng, 1);
StringBuffer sb = new StringBuffer();
if(addresses.size() > 0) {
Address address = addresses.get(0);
for(int i=0; i<address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append(“\n”);
}
sb.append(address.getLocality(i)).append(“\n”);
sb.append(address.getPostalCode(i)).append(“\n”);
sb.append(address.getCountryName(i)).append(“\n”);
addressInfo = sb.toString();
}
} catch(IOException ex) { }
}
textview.setText(“你当前的位置如下:\n”+latLng+"\n”+addressInfo);
}
protected boolean isRouteDisplayed() {
return false;
}
private final LocationListener locationListener = new LocationListener() {
// 坐标改变时调用
public void onLocationChanged(Location loaction) {
updateWithNewLocation(location);
}
// Provider禁用时调用
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
// Provider启用时调用
public void onProviderEnabled(String provider) { }
// Provider状态发生变化时调用
public void onStatusChanged(String provider, int status, Bundle extras) { }
};
// 添加菜单
public boolean onCreateOptionMenu(Menu menu) {
super.onCreateOptionMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, “放大”);
menu.add(0, ZOOM_OUT, Menu.NONE, “缩小”);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case ZOOM_IN:
// 放大
mapController.zoomIn();
return true;
case ZOOM_OUT:
// 缩小
mapController.zoomOut();
return true;
return true;
}
class LocationOverlay extends Overlay {
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
// 绘制图标文字等信息
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point point = new Point();
GeoPoint geoPoint = new GeoPoint(location.getLatitude() * 1E6, location.getLongitude() * 1E6);
mapView.getProjection().toPixels(geoPoint, point);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.home);
canvas.drawBitmap(bitmap, point.x, point.y, point);
canvas.drawText(“I’m here.”, point.x, point.y, point);
return true;
}
}
}
}
Android开发之定位系统的更多相关文章
- Android开发权威指南(第2版)新书发布
<Android 开发权威指南(第二版)>是畅销书<Android开发权威指南>的升级版,内容更新超过80%,是一本全面介绍Android应用开发的专著,拥有45 章精彩内容供 ...
- 使用Visual Studio进行 Android开发的十大理由
[原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...
- Qt开发北斗定位系统融合百度地图API及Qt程序打包发布
Qt开发北斗定位系统融合百度地图API及Qt程序打包发布 1.上位机介绍 最近有个接了一个小型项目,内容很简单,就是解析北斗GPS的串口数据然后输出经纬度,但接过来觉得太简单,就发挥了主观能动性,增加 ...
- Android学习探索之Java 8 在Android 开发中的应用
前言: Java 8推出已经将近2年多了,引入很多革命性变化,加入了函数式编程的特征,使基于行为的编程成为可能,同时减化了各种设计模式的实现方式,是Java有史以来最重要的更新.但是Android上, ...
- Android 开发一定要看的15个实战项目
前言: 虽说网上有太多的Android课程,但是大多都是视频,有Android在线开发环境的几乎没有,但是对于学习Android的人来说拥有在线的Android开发环境是非常好的,可以随时动手操作学习 ...
- Android开发学习之路-关于Exception
Exception在Java中是表示异常的一个类.它是Throwable的子类. 而Exception的子类RuntimeException是一个特殊的异常类,在代码中不需要对此类进行throw,而是 ...
- Android开发学习之路-Android中使用RxJava
RxJava的核心内容很简单,就是进行异步操作.类似于Handler和AsyncTask的功能,但是在代码结构上不同. RxJava使用了观察者模式和建造者模式中的链式调用(类似于C#的LINQ). ...
- Android开发学习之路-记一次CSDN公开课
今天的CSDN公开课Android事件处理重难点快速掌握中老师讲到一个概念我觉得不正确. 原话是这样的:点击事件可以通过事件监听和回调两种方法实现. 我一听到之后我的表情是这样的: 这跟我学的看的都不 ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
随机推荐
- HDU 5640 King's Cake GCD
King's Cake 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5640 Description It is the king's birthd ...
- Linux使用C语言链接MsSQL
1.安装gcc编译器 yum install gcc 2.下载freetds wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched ...
- Delphi TFileStream 打开模式与共享模式
{ TFileStream create mode } fmCreate = $FF00; { Create a file with the given name. If a file with th ...
- How to Distinguish a Physical Disk Device from an Event Message
https://support.microsoft.com/en-us/help/159865 https://support.microsoft.com/en-us/help/244780/inf ...
- LeetCode89:Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- node升级后,项目中的node-sass报错的问题
之前可能因为电脑不知道哪抽风了,在npm build的时候进入就卡在入口的地方,启动express的时候也会,所以就重装了一下node 重装node 其实也不是重装,就是使用 where node 查 ...
- 简单的后台管理系统vue-cli3.0+element-ui
前段时间在研究一个分发系统,发现vue-cli+element-ui好像是挺不错的,然后自己根据那个分发系统尝试搭建了一下 1.首先安装vue和vue-cli // 全局安装vue npm insta ...
- 神经网络可以拟合任意函数的视觉证明A visual proof that neural nets can compute any function
One of the most striking facts about neural networks is that they can compute any function at all. T ...
- HDU 4901 The Romantic Hero 题解——S.B.S.
The Romantic Hero Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- iOS网络编程解析协议三:JSON数据传输解析
作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式 基于JavaScript的一个子集 易读性略差,编码手写难度大,数据量小 JSON格式取代了XML给网络传输带来了很大的便利, ...