android 获取GPS定位(原文地址)
来源:互联网 发布:龙岗大学 知乎 编辑:程序博客网 时间:2024/05/01 16:39
Android的强大表现在各个方面,在这里介绍一下其中的一个自动获取所在地理位置坐标的功能。Android中通过LocationManager来获取地理位置等相关信息的。
首先,需要获取LocationManager实例。
//获得当前位置的坐标
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);//获取LocationManager的一个实例
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, 0, locationListener);
/*注册一个周期性的位置更新 每隔1000ms更新一次,并且不考虑位置的变化。
最后一个参数是LocationListener的一个引用*/
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
String latitude = Double.toString(location.getLatitude());//经度
String longitude = Double.toString(location.getLongitude());//纬度
String altitude = Double.toString(location.getAltitude());//海拔
//输出文字
TextView tv = (TextView) this.findViewById(R.id.local);
tv.setText("latitude:"+latitude+" longitude:"+longitude
+" altitude:"+altitude+" ");
实现LocationListener的引用
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
if (location != null) {
String latitude = Double.toString(location.getLatitude());//经度
String longitude = Double.toString(location.getLongitude());//纬度
String altitude = Double.toString(location.getAltitude());//海拔
//输出文字
TextView tv = (TextView) findViewById(R.id.local);
tv.setText("latitude:"+latitude+" longitude:"
+longitude+" altitude:"+altitude+" ");
}
}
public void onProviderDisabled(String provider) {
第1页 第2页
Android的强大表现在各个方面,在这里介绍一下其中的一个自动获取所在地理位置坐标的功能。Android中通过LocationManager来获取地理位置等相关信息的。
首先,需要获取LocationManager实例。
//获得当前位置的坐标
LocationManager locationManager = (LocationManager)
getSystemService(LOCATION_SERVICE);//获取LocationManager的一个实例
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, 0, locationListener);
/*注册一个周期性的位置更新 每隔1000ms更新一次,并且不考虑位置的变化。
最后一个参数是LocationListener的一个引用*/
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
String latitude = Double.toString(location.getLatitude());//经度
String longitude = Double.toString(location.getLongitude());//纬度
String altitude = Double.toString(location.getAltitude());//海拔
//输出文字
TextView tv = (TextView) this.findViewById(R.id.local);
tv.setText("latitude:"+latitude+" longitude:"+longitude
+" altitude:"+altitude+" ");
实现LocationListener的引用
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
if (location != null) {
String latitude = Double.toString(location.getLatitude());//经度
String longitude = Double.toString(location.getLongitude());//纬度
String altitude = Double.toString(location.getAltitude());//海拔
//输出文字
TextView tv = (TextView) findViewById(R.id.local);
tv.setText("latitude:"+latitude+" longitude:"
+longitude+" altitude:"+altitude+" ");
}
}
public void onProviderDisabled(String provider) {
第1页 第2页
接上页
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};
最后我们在AndroidManifest.xml中加入GPS权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
如果是在模拟器中调试,可以打开”Window” ?>”Show View” 中打开”Emulator Control” View即可手动设置,或通过KML和GPX文件来设置一个坐标。
或者使用geo命令,开始?> 运行?>输入 telnet 5554,然后在命令行下输入 geo fix -39.4 116.9 326 ,这三个参数分别代表了经度、纬度和海拔(海拔可不写)
这样我们就可以获取所在位置的坐标了
- android 获取GPS定位(原文地址)
- android 获取GPS定位,
- Android GPS定位(获取经纬度)
- Android GPS定位(获取经纬度)
- Android获取GPS定位数据
- Android GPS定位 获取经纬度
- Android GPS定位获取当前位置
- Android GPS定位 获取经纬度
- Android GPS定位 获取经纬度
- Android开发中定位解析(GPS定位、网络定位、位置地址解析)
- Android获取当前位置(GPS和网络定位)
- Android获取当前位置(GPS和网络定位)
- Android GPS/WIFI定位 获取经纬度
- android网络-GPS获取定位信息
- Android APP - GPS定位并获取地理位置
- Android开发之GPS定位获取位置
- Android 获取GPS和网络定位信息
- xamarin android使用gps定位获取经纬度
- cocos2d-x:菜单项
- JDBC第四讲:JDBC知识扩展
- Android Bitmap处理
- main函数的两个参数
- android,自定义广播,最终广播接收者,防止拦截广播,有序广播,无序广播
- android 获取GPS定位(原文地址)
- XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
- cocoa2D-x:helloWorld(1)
- 【数据结构】对于线性表的顺序和链接两种存储方式的主要优缺点
- Java Process中waitFor()的问题(使用Runtime.getRuntime().exec(...)时)
- How To Add Swap on CentOS 6
- 容器组件 和awt组件
- 面试 linux常用命令
- [个人开发者赚钱四]开发产品及发布到应用市场