本文主要是收集了一些相关的获取当前位置信息的方法,是为以后的查询所用,由于时间仓促还未检验是否有效,希望各位博友看后点评,纠正错误,谢谢
(1)
一般来讲,通过gps获取到经纬度坐标以后,要继续深入的获取该经纬度坐标的城市、街道与精度(误差)等信息。
private String getAddressbyGeoPoint() {
// 自经纬度取得地址
StringBuilder sb = new StringBuilder();
Geocoder gc = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> lstAddr = null;
try {
lstAddr = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException e) {
Log.e("HangzhouBike", e.getMessage());
}
if (lstAddr != null && lstAddr.size() > 0) {
Address addr = lstAddr.get(0);
if (addr.getAddressLine(1) != null) {
sb.append(addr.getAddressLine(1)).append(" ");
}
if (addr.getAddressLine(2) != null){
sb.append(addr.getAddressLine(2)).append(" ");
sb.append(" ±" + location.getAccuracy() + "米");
}、
}
return sb.toString();
}
(2)
Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置;注册/注销来自某个 LocationProvider的周期性的位置更新;以及注册/注销接近某个坐标时对一个已定义Intent的触发等。今天我们就来看看Android 中LocatinManager的简单使用,以获取当前所在的位置为例。
首先,我们需要获取LocationManager的一个实例,这里需要注意的是他的实例只能通过下面这种方式来获取,直接实例化LocationManager是不被允许的。
- LocationManagerlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
得到了LocationManager的实例locatonManager以后,我们通过下面的语句来注册一个周期性的位置更新。
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
-
1000,0,locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener);
这句代码告诉系统,我们需要从GPS获取位置信息,并且是每隔1000ms更新一次,并且不考虑位置的变化。最后一个参数是LocationListener的一个引用,我们必须要实现这个类。
-
privatefinalLocationListenerlocationListener=newLocationListener(){
-
publicvoidonLocationChanged(Locationlocation){
-
if(location!=null){
- Log.i("SuperMap","Locationchanged:Lat:"
- +location.getLatitude()+"Lng:"
- +location.getLongitude());
- }
- }
-
-
publicvoidonProviderDisabled(Stringprovider){
- }
-
-
publicvoidonProviderEnabled(Stringprovider){
- }
-
-
publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){
- }
- };
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) { //当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
// log it when the location changes
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
// Provider被disable时触发此函数,比如GPS被关闭
}
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};
以上的这些步骤一般应当在Activity的onCreate()阶段完成。
在成功注册了一个周期性坐标更新以后,我们就随时可以通过下面的方法来取得当前的坐标了。
- Locationlocation=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
-
doublelatitude=location.getLatitude();
-
doublelongitude=location.getLongitude();
-
doublealtitude=location.getAltitude();
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude(); //经度
double longitude = location.getLongitude(); //纬度
double altitude = location.getAltitude(); //海拔
不过这时候,如果你尝试去运行这个LocationSample的话程序启动时多半就会报错,因为我们没有设置GPS相关的权限,解决方法也相当简单,在AndroidManifest.xml中的block里添加下面这句即可解决权限的问题。详细的权限设置,请参考官方文档docs/reference/android/Manifest.permission.html
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
(3)
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
/* TODO - adjust update times to minimize battery use. Can be changed as needed.
*
*/
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}
Obviously you don't need to request location updates, but if you do then below is some code I use....
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location newloc) {
// something to do here when location changes
// TODO get other datq (accuracy, velocity, altitude, etc...)
// write data to database
// add markers
location = newloc;
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
and you need to remember to turn off/on the updating at onStart and onStop
@Override
public void onStop() {
super.onStop();
if (provider != null) {
locationManager.removeUpdates(locationListener);
}
@Override
public void onStart() {
super.onStart();
/* recheck which provider to use */
provider = locationManager.getBestProvider(criteria,true);
if (provider != null) {
location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider,
60000, // 1min
100, // 100m
locationListener);
}
}
分享到:
相关推荐
很多下载店用户电影用连续都在一个文件夹下,这样文件整理方式极为不方便,为了便于在优影下载系统中下载和计费,需要每一部电影和连续剧建立一个文件夹,方便下载。 七、文件打印目录生成(增加图片打印册生成 和 ...
{8.1}线程的常用属性与方法}{121}{section.8.1} {8.2}后台线程}{123}{section.8.2} {8.3}创建线程的两种方法}{123}{section.8.3} {8.4}Runnable}{123}{section.8.4} {8.5}Sleep阻塞与打断唤醒}{124}{section....
计算机网络安全问题及防范措施 计算机病毒是一组能破坏计算机功能或者数据,可以自我复制的一组计算机指令或者程序代码,下面是搜集整理的一篇探究计算机网络安全问题的,欢迎阅读参考。 摘要:随着计算机技术和网络...
在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...
内网的常见泄密方式包括:通过局域网内的某台主机,利用网络欺骗或攻击方式非法获取其他主机或网络服务器的重要数据;利用内网网络交换设备或直接使用网线非法接入计算机终端、内网等窃取数据;内部工作人员盗取管理员...
22.常用的调用WebService的方法有哪些? 答:1.使用WSDL.exe命令行工具。 2.使用VS.NET中的Add Web Reference菜单选项 23..net Remoting 的工作原理是什么? 答:服务器端向客户端发送一个进程编号,一个程序域...
【选区背景选择】 进行着色功能切换,支持行着色、列着色、行与列同时着色、智能着色、选区着色、反向着色共6种方式供选择,可以自定义56种基本及颜色板任意着色颜色。 【条件背景】 默认启动智能着色方式,此功能...
《Java开发实战1200例》分为I、II两卷共计1200个例子,包括了开发中各个方面最常用的实例,是目前市场上实例最全面的开发类图书;书中实例来源于多位工程师的多年积累,具有很强的实用性。 本书是第II卷,以开发...
如果有就直接从缓存中获取方法调用后的结果,如果没有就调用方法 并缓存结果后返回给用户。下 次调用直接从缓存中获取。 • 使用Spring缓存抽象时我们需要关注以下两点; 1、确定方法需要被缓存 以及他们的缓存策略 ...
Excel集成工具箱6.0是利用VBA(Visual Basic for Applications)语言编写的增强应用型插件。...【系统功能信息】 系统功能调用及相关信息,关闭电脑、重启电脑、电脑使用时间、磁盘剩余空间。 【查看...
对于多数已知漏洞,我们给出了相应的漏洞描述、解决方案及详细描述链接,其它漏洞资料正在进一步整理完善中,您也可以通过本站的“安全文摘”和“安全漏洞”栏目查阅相关说明。 3.0版本提供了简单的插件开发包,...
标记整理算法(Mark-Compact).................................................................................................. 28 分代收集算法 ..............................................................
对于多数已知漏洞,我们给出了相应的漏洞描述、解决方案及详细描述链接,其它漏洞资料正在进一步整理完善中,您也可以通过本站的“安全文摘”和“安全漏洞”栏目查阅相关说明。 3.0版本提供了简单的插件开发包,...
感谢wlj、killer、coolc协助测试,感谢通过各种方式提供反馈信息及建议的朋友。 X-Scan v3.2 -- 发布日期:04/08/2005,升级NASL库,优化主程序及NASL库;增加 HTTP/TELNET/SSH/VNC/CVS/IMAP等弱口令检测插件;...
对于多数已知漏洞,我们给出了相应的漏洞描述、解决方案及详细描述链接,其它漏洞资料正在进一步整理完善中,您也可以通过本站的“安全文摘”和“安全漏洞”栏目查阅相关说明。 3.0及后续版本提供了简单的插件开发...