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 ,这三个参数分别代表了经度、纬度和海拔(海拔可不写)
这样我们就可以获取所在位置的坐标了
第1页 第2页
相关推荐
Android LocationManager 获取经纬度和卫星信息 配文代码 可帮助熟练使用Android自带的定位api
LocationManager,实现了从网络中获取GPS信息的方法。
gps定位,实测可用. 第一个是网络定位,第二个是GPS定位,第三个是直接取缓存。...// 获取最好的定位方式 String provider = locationManager.getBestProvider(criteria, true); // true 代表从打开的设备中查找
Locationmanager Locationmanager is a small wrapper for Location Tracker. Installation Add jitpackto your repositories in Project build.gradle : allprojects { repositories { ... maven { url ...
这是一个利用LocationManager定位的实例demo,可获取经纬度,国家名称,城市名称,街道等信息。详细介绍请移步:http://blog.csdn.net/xiong_it/article/details/46968477
这是一个用户定位的示例 Main 类用于演示获取Location Provider Main2 类用于演示获取最佳的Location Provider,两个类要修改Manifest.xml 文件才能分别运行
Android Studio 实训操作题(五)实验7LocationManager基本用法.docx
主要介绍了Android LocationManager获取经度与纬度等地理信息的相关资料,希望通过本站大家能掌握这样的知识,需要的朋友可以参考下
位置测试 使用 LocationManager API 获取安卓手机位置
android LocationManager 研究资料
Android LocationManager获取经纬度信息,在不使用第三方地图SDK的情况下,也可以借助Android 本身的定位API来实现相关地理位置功能,相关博文见这里https://blog.csdn.net/diyangxia/article/details/90674393
摘要:Java源码,Android,Android源码 Android源码位置管理简单应用,涉及定位,LocationManager源代码下载,源码爱好者Java源码频道为您提供丰富的Android开发小实例,助想学习Android程序设计的朋友一臂之力,本...
LocationManager,
现在有这么一个需求:开启一个Service服务,获取当前...Android手机获取当前位置的方式:GPS定位,WIFI定位,基站定位,当前Demo使用GPS卫星定位,在LocationTool中返回Location、LocationManager两者对象,通过Loca
一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),...
在Android开发当中,经常需要用到定位功能,尤其是依赖于地理位置功能的应用,本文介绍了Android通过原生APi获取所在位置的经纬度,分享给大家 一、难点介绍 1.难点 我们的应用要新增一个功能,就是在用户打开附件...
如何得到的位置,而无需使用GPS 使用LocationManager,获取当前的位置。
import android.location.LocationManager;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView; public class MainActivity extends Activ
虽然它可以很好地跟踪用户位置随时间的变化(例如用于逐向导航),但是正确请求单个位置更新(例如确定用户当前城市以获取天气)非常麻烦预测,或从当前位置自动填充地址)。 INTULocationManager可以轻松地同时或...