android定位的4种方式介绍
android定位一般有四种方法,这四种方式分别是:GPS定位,WIFI定准,基站定位,AGPS定位,
(1)AndroidGPS:需要GPS硬件支持,直接和卫星交互来获取当前经纬度,这种方式需要手机支持GPS模块(现在大部分的智能机应该都有了)。通过GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电;2,绝大部分用户默认不开启GPS模块;3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间;4,室内几乎无法使用。这其中,缺点2,3都是比较致命的。需要指出的是,GPS走的是卫星通信的通道,在没有网络连接的情况下也能用。
要实用Adnroid平台的GPS设备,首先需要添加上权限,所以需要添加如下权限:
uses-permissionandroid:name=android.permission.ACCESS_FINE_LOCATION/uses-permission
具体实现代码如下:
首先判断GPS模块是否存在或者是开启:
privatevoidopenGPSSettings(){ LocationManageralm=(LocationManager)this .getSystemService(Context.LOCATION_SERVICE); if(alm .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)){ Toast.makeText(this,GPS模块正常,Toast.LENGTH_SHORT) .show(); return; } Toast.makeText(this,请开启GPS!,Toast.LENGTH_SHORT).show(); Intentintent=newIntent(Settings.ACTION_SECURITY_SETTINGS); startActivityForResult(intent,0);//此为设置完成后返回到获取界面 }
如果开启正常,则会直接进入到显示页面,如果开启不正常,则会进行到GPS设置页面:
获取代码如下:
privatevoidgetLocation() { //获取位置管理服务 LocationManagerlocationManager; StringserviceName=Context.LOCATION_SERVICE; locationManager=(LocationManager)this.getSystemService(serviceName); //查找到服务信息 Criteriacriteria=newCriteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗 Stringprovider=locationManager.getBestProvider(criteria,true);//获取GPS信息 Locationlocation=locationManager.getLastKnownLocation(provider);//通过GPS获取位置 updateToNewLocation(location); //设置监听*器,自动更新的最小时间为间隔N秒(1秒为1*1000,这样写主要为了方便)或最小位移变化超过N米 locationManager.requestLocationUpdates(provider,100*1000,500, locationListener);}
到这里就可以获取到地理位置信息了,但是还是要显示出来,那么就用下面的方法进行显示:
代码
privatevoidupdateToNewLocation(Locationlocation){ TextViewtv1; tv1=(TextView)this.findViewById(R.id.tv1); if(location!=null){ doublelatitude=location.getLatitude(); doublelongitude=location.getLongitude(); tv1.setText(维度:+latitude+\n经度+longitude); }else{ tv1.setText(无法获取地理信息); } }
publicclassWiFiInfoManagerimplementsSerializable{ privatestaticfinallongserialVersionUID=-4582739827003032383L; privateContextcontext; publicWiFiInfoManager(Contextcontext){ super(); this.context=context; } publicWifiInfogetWifiInfo(){ WifiManagermanager=(WifiManager)context .getSystemService(Context.WIFI_SERVICE); WifiInfoinfo=newWifiInfo(); info.mac=manager.getConnectionInfo().getBSSID(); Log.i(TAG,WIFIMACis:+info.mac); returninfo; } publicclassWifiInfo{ publicStringmac; publicWifiInfo(){ super(); } } }
上面是取到WIFI的mac地址的方法,下面是把地址发送给google服务器,代码如下
publicstaticLocationgetWIFILocation(WifiInfowifi){ if(wifi==null){ Log.i(TAG,wifiisnull.); returnnull; } DefaultHttpClientclient=newDefaultHttpClient(); HttpPostpost=newHttpPost(http://www.google.com/loc/json); JSONObjectholder=newJSONObject(); try{ holder.put(version,1.1.0); holder.put(host,maps.google.com); JSONObjectdata; JSONArrayarray=newJSONArray(); if(wifi.mac!=nullwifi.mac.trim().length()0){ data=newJSONObject(); data.put(mac_address,wifi.mac); data.put(signal_strength,8); data.put(age,0); array.put(data); } holder.put(wifi_towers,array); Log.i(TAG,requestjson:+holder.toString()); StringEntityse=newStringEntity(holder.toString()); post.setEntity(se); HttpResponseresp=client.execute(post); intstate=resp.getStatusLine().getStatusCode(); if(state==HttpStatus.SC_OK){ HttpEntityentity=resp.getEntity(); if(entity!=null){ BufferedReaderbr=newBufferedReader( newInputStreamReader(entity.getContent())); StringBuffersb=newStringBuffer(); Stringresute=; while((resute=br.readLine())!=null){ sb.append(resute); } br.close(); Log.i(TAG,responsejson:+sb.toString()); data=newJSONObject(sb.toString()); data=(JSONObject)data.get(location); Locationloc=newLocation( android.location.LocationManager.NETWORK_PROVIDER); loc.setLatitude((Double)data.get(latitude)); loc.setLongitude((Double)data.get(longitude)); loc.setAccuracy(Float.parseFloat(data.get(accuracy) .toString())); loc.setTime(System.currentTimeMillis()); returnloc; }else{ returnnull; } }else{ Log.v(TAG,state+); returnnull; } }catch(Exceptione){ Log.e(TAG,e.getMessage()); returnnull; } }
4.AGPS定位
AGPS(AssistedGPS:辅助全球卫星定位系统)是结合GSM或GPRS与传统卫星定位,利用基地台代送辅助卫星信息,以缩减GPS芯片获取卫星信号的延迟时间,受遮盖的室内也能借基地台讯号弥补,减轻GPS芯片对卫星的依赖度。和纯GPS、基地台三角定位比较,AGPS能提供范围更广、更省电、速度更快的定位服务,理想误差范围在10公尺以内,日本和美国都已经成熟运用AGPS于LBS服务(LocationBasedService,基于位置的服务)。AGPS技术是一种结合了网络基站信息和GPS信息对移动台进行定位的技术,可以在GSM/GPRS、WCDMA和CDMA2000网络中使进行用。该技术需要在手机内增加GPS接收机模块,并改造手机的天线,同时要在移动网络上加建位置服务器、差分GPS基准站等设备。AGPS解决方案的优势主要体现在其定位精度上,在室外等空旷地区,其精度在正常的GPS工作环境下,可以达到10米左右,堪称目前定位精度最高的一种定位技术。该技术的另一优点为:首次捕获GPS信号的时间一般仅需几秒,不像GPS的首次捕获时间可能要2~3分钟
相关文章
- android 浏览器 开发,Android 浏览器的开发实例分享
- android签名命令行,Android系统签名位置及命令
- android 的hook技术,Android Native Hook技术(一)
- strictmode android,Android中的StrictMode
- android短信验证码方案,Android之短信验证码
- Android listview中使用checkbox「建议收藏」
- Android angle_android 界面悬停
- android 6.0锁屏界面时间位置修改[通俗易懂]
- 自研的内存分析利器开源了!Android Bitmap Monitor 助你定位不合理的图片使用
- 原创Paper | 在 Android 中开发 eBPF 程序学习总结(二)
- android studio中关于图片报错相关问题总结
- Android studio更新后出现警告:Warning:The `android.dexOptions.incremental` property is deprecated and it has
- 【Android NDK 开发】Visual Studio 2019 使用 CMake 开发 JNI 动态库 ( 动态库编译配置 | JNI 头文件导入 | JNI 方法命名规范 )
- 【Android 应用开发】Canvas 绘制文字 ( 文字尺寸测量 | 基线绘制 )
- 【错误记录】前台进程报错 ( Permission Denial: startForeground requires android.permission.FOREGROUND_SERVICE )
- 【Flutter】Flutter 全面屏适配 ( 需要适配的情况 | Android 设置最大宽高比 | 使用 SafeArea 进行全面屏适配 | 使用 MediaQuery 进行全面屏适配 )
- 【ijkplayer】编译 Android 版本的 ijkplayer ⑥ ( 进入 ijkplayer-android/android 目录 | 执行 compile-ijk.sh 脚本完成编译 )
- 【Android NDK 开发】CMake 中查找链接 Android 自带动态库位置说明 ( ndk-bundleplatformsandroid-29arch-armusrlib )
- Android控制文字水平间距android:letterSpacing详解手机开发
- [android] 显示意图激活另外一个activity详解手机开发
- android EventBus详解(一)手机开发
- java Android 检查当前是否已经连接上 Internet详解编程语言
- Android检查SD卡是否可读写详解编程语言
- 针对Android沙盒的“中间APP攻击”
- 乐播科技冯森:我为什么不看好Android电视游戏
- android如何默认打开小区广播具体实现
- android中DatePicker和TimePicker的使用方法详解
- Android学习笔记(二)App工程文件分析
- Android中的应用认领总结