Android獲取位置信息


1.AndroidMainfext.xml添加權限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

2.AndroidMainfext.xml 加入android:targetSdkVersion="23" (為什么要加入這個是為了配合安卓6.0以上系統需要動態申請權限,加入這個不寫或者低於23則23以及以上的會無法獲得權限)
3.獲取定位服務以及當前可用的控制器

private static LocationListener locationListener = new LocationListener() { @Override public void onLocationChanged(Location location) { if(location != null){ String string = "緯度為:" + location.getLatitude() + ",經度為:"+ location.getLongitude(); Log.i(logTag,"string"+string); AndroidLocation.getAddress(location); AndroidLocation.onActivityStoped(); } } @Override public void onProviderDisabled(String arg0) { } @Override public void onProviderEnabled(String arg0) { } @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { } }; public static void startLocation(Context context){ mContext = context; //獲取定位服務 m_locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); //獲取當前可用的位置控制器 List<String> list = m_locationManager.getProviders(true); if (list.contains(LocationManager.GPS_PROVIDER)) { //是否為GPS位置控制器 m_provider = LocationManager.NETWORK_PROVIDER;//NETWORK_PROVIDER GPS_PROVIDER Log.i(logTag,"is GPS"); } else if (list.contains(LocationManager.NETWORK_PROVIDER)) { //是否為網絡位置控制器 m_provider = LocationManager.NETWORK_PROVIDER; Log.i(logTag,"is network"); } if(m_provider != null){ Location location = m_locationManager.getLastKnownLocation(m_provider); if(location!=null){ //AndroidLocation.getAddress(location); //直接獲取 }else{ //沒有需要加監聽等待獲取 m_locationManager.requestLocationUpdates(m_provider, 3000, 1, locationListener); } } } 

4.在activity的生命周期內要記得刪除listener,我的需求是獲取到地址就可以了不需要監聽位置的變化所以獲取到了直接就remove掉了

public static void onActivityStoped(){ if(locationListener != null){ m_locationManager.removeUpdates(locationListener); locationListener = null; } Log.i(logTag,"onActivityStoped"); } 

5.以上獲得的是經緯度,這個還需要一次轉化,轉化為我們熟悉的XX省XX市XX街道這種

 private static String getAddress(Location location){ //用來接收位置的詳細信息 List<Address> result = null; String addressLine = ""; try { if (location != null) { Geocoder gc = new Geocoder(mContext, Locale.getDefault()); result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); } } catch (Exception e) { e.printStackTrace(); } if(result != null && result.get(0) != null){ //這塊獲取到的是個數組我們取一個就好 下面是具體的方法查查API就能知道自己要什么 //result.get(0).getCountryName() Log.i("address",addressLine); //Toast.makeText(mContext,result.get(0).toString(),Toast.LENGTH_LONG).show(); } return addressLine; } 

6.做到這里完成了一大半了 試着去獲取下也有了,但是但是但是重要的事情說三遍安卓6.0以上的系統獲取不到(悲傷地表情,查了好多地方也就介紹到這里了)我們繼續
我們要做的是前面說的動態加載權限代碼如下:

public static void initPermission(Context context) { if (Build.VERSION.SDK_INT >= 23) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //請求權限 ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_LOCATION); }else{ startLocation(context); } }else{ startLocation(context); } } 

但是加上這個就好了嗎?顯然並不是實際上加上這個病不起什么作用;關鍵在如下:
一定要實現OnRequestPermissionsResultCallback 在權限結果有了之后再初始化定位(AndroidLocation.REQUEST_CODE_LOCATION 是requestPermissions申請權限的一個自己定義的變量隨便寫)

import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat.OnRequestPermissionsResultCallback; public class MainActivity extends Activity implements OnRequestPermissionsResultCallback{ ... @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { // TODO Auto-generated method stub if(grantResults.length == 0){ return; } Log.i("AndroidLocation.REQUEST_CODE_LOCATION",AndroidLocation.REQUEST_CODE_LOCATION+"==="+requestCode); switch (requestCode) { case AndroidLocation.REQUEST_CODE_LOCATION: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Toast.makeText(MainActivity.this, "授權位置信息 Denied", Toast.LENGTH_SHORT) // .show(); location.init(this); } break; default: //super.onRequestPermissionsResult(requestCode, permissions, grantResults); } ... } 

原理很簡單,做個備忘!

 鏈接:https://www.jianshu.com/p/783fb8580dd4

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM