一般我們使用的是第三方定位第队,因?yàn)榈谌蕉ㄎ槐容^成熟,有些場(chǎng)景我們不需要或者不可以使用第三方定位的時(shí)候我們就需要自己去獲取定位了与学。
定位方式
1.gps和network(時(shí)效性差)
1.1 使用條件
1.1.1 需要開(kāi)啟位置服務(wù):
高精準(zhǔn)度(GPS+Network)
節(jié)電(network)
僅限設(shè)備(gps)
1.1.2 安卓6.0以上還需要開(kāi)啟位置權(quán)限android.permission.ACCESS_COARSE_LOCATION胚迫,android.permission.ACCESS_FINE_LOCATION
1.2 代碼部分
gps為衛(wèi)星定位薯定,室外定位。network室內(nèi)也可以定位埠帕,但是精準(zhǔn)度不高忌傻。在我們必須要快速獲取位置信息的時(shí)候,我們加上IP定位和基站定位效果會(huì)好一些
1.2.1 定位工具類(lèi)
/**
* @author 程前 on 2018/9/26.
* blog: https://blog.csdn.net/ch1406285246
* content:
* modifyNote:
*/
object LocationUtils {
var latitude = 0.0
var longitude = 0.0
/**
* 獲取經(jīng)緯度
*
* @param context
* @return
*/
@SuppressLint("MissingPermission")
fun getLngAndLat(context: Context): String {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val providers = locationManager.allProviders
for (provider in providers) {
val location = locationManager.getLastKnownLocation(provider)
if (location != null) {
latitude = location.latitude
longitude = location.longitude
return "$longitude,$latitude"
}
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0F, locationListener)
val location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (location != null) {
latitude = location.latitude
longitude = location.longitude
}
return "$longitude,$latitude"
}
val locationListener = object : LocationListener {
override fun onLocationChanged(location: Location?) {
if (location != null) {
latitude = location.latitude;
longitude = location.longitude;
}
}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {
}
override fun onProviderEnabled(provider: String?) {
}
override fun onProviderDisabled(provider: String?) {
}
};
/**
* 判斷GPS是否開(kāi)啟搞监,GPS或者AGPS開(kāi)啟一個(gè)就認(rèn)為是開(kāi)啟的
* @param context
* @return true 表示開(kāi)啟
*/
fun isOPen(context: Context): Boolean {
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
// 通過(guò)GPS衛(wèi)星定位水孩,定位級(jí)別可以精確到街(通過(guò)24顆衛(wèi)星定位,在室外和空曠的地方定位準(zhǔn)確琐驴、速度快)
val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
// 通過(guò)WLAN或移動(dòng)網(wǎng)絡(luò)(3G/2G)確定的位置(也稱(chēng)作AGPS俘种,輔助GPS定位秤标。主要用于在室內(nèi)或遮蓋物(建筑群或茂密的深林等)密集的地方定位)
val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (gps || network) {
return true;
}
return false;
}
}
1.2.2 檢查定位服務(wù)
/**
* 檢查定位服務(wù)有沒(méi)有開(kāi)啟,沒(méi)有開(kāi)啟就引導(dǎo)用戶(hù)去開(kāi)啟定位服務(wù)
* */
private fun checkLocation() {
if (!LocationUtils.isOPen(this)) {
//沒(méi)有打開(kāi)則彈出對(duì)話框
//下面是個(gè)dialog宙刘,請(qǐng)自己寫(xiě)苍姜,我的封裝dialog不貼出來(lái)
val tipDialog = TipDialog(this)
.setTitle("提示")
.setContent("當(dāng)前應(yīng)用需要打開(kāi)定位功能。\n\n請(qǐng)點(diǎn)擊\"設(shè)置\"-\"定位服務(wù)\"-打開(kāi)定位功能悬包。")
.setLeftBtnText("取消")
.setRightBtnText("去設(shè)置")
tipDialog.setOnBtnClickListener(object : TipDialog.OnDialogBtnClickListener {
override fun onLeftBtnClicked(paramTipDialog: TipDialog?) {
tipDialog.dismiss()
}
override fun onRightBtnClicked(paramTipDialog: TipDialog?) {
tipDialog.dismiss()
//跳轉(zhuǎn)GPS設(shè)置界面
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, GPS_REQUEST_CODE);
}
}).show()
}
}
1.2.3 設(shè)置完成之后獲取經(jīng)緯度信息
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == GPS_REQUEST_CODE) {
//做需要做的事情衙猪,比如再次檢測(cè)是否打開(kāi)GPS了 或者定位
checkLocation()
}else{
val locationStr=LocationUtils.getLngAndLat(this)
}
}
2.IP定位(準(zhǔn)確性差)
訪問(wèn)API拿到IP信息测砂,例http://pv.sohu.com/cityjson?ie=utf-8
IP定位.png
如果你僅僅只是需要城市堵幽,那么這樣做足夠了,如果你還要更詳細(xì)的信息聪姿,請(qǐng)使用IP查詢(xún)地理位置信息的API撑瞧。