Android 调用原生API获取地理位置和经纬度,判断所在国家
生活随笔
收集整理的這篇文章主要介紹了
Android 调用原生API获取地理位置和经纬度,判断所在国家
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public static boolean isCN(Context context) {TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);String countryIso = tm.getSimCountryIso();boolean isCN = false;//判斷是不是大陸if (!TextUtils.isEmpty(countryIso)) {countryIso = countryIso.toUpperCase(Locale.US);if (countryIso.contains("CN")) {isCN = true;}}return isCN;}
/** 判斷是否是國內的 SIM 卡,優先判斷注冊時的mcc */
public static boolean isChinaSimCard(Context c) {String mcc = getSimOperator(c);if (isOperatorEmpty(mcc)) {return false;} else {return mcc.startsWith("460");}
}
?
Locale locale = Locale.getDefault(); String country = locale.getCountry();?CN? ?就是中國
1、添加位置權限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>2、activity實現獲取經緯度,地理位置代碼
public class MainActivity extends AppCompatActivity {private TextView textView;private static final String[] authBaseArr = {//申請類型Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION};private static final int authBaseRequestCode = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = findViewById(R.id.textView);LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);initNavi();//權限檢查的代碼if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {// TODO: Consider callingreturn;}locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,//指定GPS定位提供者1000,//指定數據更新的間隔時間1,//位置間隔的距離為1mnew LocationListener() {//監聽GPS信息是否改變@Overridepublic void onLocationChanged(Location location) {//GPS信息發送改變時回調Log.i("lgq","onLocationChanged===="+location.getProvider());}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {//GPS狀態發送改變時回調}@Overridepublic void onProviderEnabled(String provider) { //定位提供者啟動時回調}@Overridepublic void onProviderDisabled(String provider) { //定位提供者關閉時回調}});Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);//獲取最新的定位信息locationUpdates(location);}private boolean hasBasePhoneAuth() {PackageManager pm = getPackageManager();for (String auth : authBaseArr) {if (pm.checkPermission(auth, getPackageName()) != PackageManager.PERMISSION_GRANTED) {return false;}}return true;}private void initNavi() {// 申請權限if (android.os.Build.VERSION.SDK_INT >= 23) {if (!hasBasePhoneAuth()) {this.requestPermissions(authBaseArr, authBaseRequestCode);return;}}}public void locationUpdates(Location location){if(location != null){StringBuilder stringBuilder = new StringBuilder(); //構建一個字符串構建器,用于記錄定位信息stringBuilder.append("您的位置是:\n");stringBuilder.append("經度:");stringBuilder.append(location.getLongitude());stringBuilder.append("\n緯度:");stringBuilder.append(location.getLatitude());textView.setText(stringBuilder.toString());Log.i("lgq",".....經度==="+location.getLongitude()+"...緯度+====="+location.getLatitude());String ab = getAddress(location.getLatitude(),location.getLongitude());Log.i("lgq","sssssfa===="+ab);}else{textView.setText("GPS失效啦...");}}public String getAddress(double latitude, double longitude) {Geocoder geocoder = new Geocoder(this, Locale.getDefault());try {List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 1);// Address[addressLines=[0:"廣東省東莞市健升大廈"],feature=健升大廈,admin=廣東省,sub-admin=null,locality=東莞市,thoroughfare=null,postalCode=null,countryCode=CN,countryName=中國,hasLatitude=true, // latitude=23.025354,hasLongitude=true,longitude=113.748738,phone=null,url=null,extras=Bundle[mParcelledData.dataSize=92]]if (addresses.size() > 0) {Address address = addresses.get(0);String data = address.toString();int startCity = data.indexOf("locality=") + "locality=".length();int endCity = data.indexOf(",", startCity);String city = data.substring(startCity, endCity);int startPlace = data.indexOf("feature=") + "feature=".length();int endplace = data.indexOf(",", startPlace);String place = data.substring(startPlace, endplace);return city + place ;}} catch (IOException e) {e.printStackTrace();}return "獲取失敗";} }如獲取不到位置信息
加一判斷即可
if (location==null){locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,//指定GPS定位提供者5000,//指定數據更新的間隔時間10,//位置間隔的距離為1mnew LocationListener() {//監聽GPS信息是否改變@Overridepublic void onLocationChanged(Location location) {//GPS信息發送改變時回調Log.i("lgq","onLocationChanged===="+location.getProvider());}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {//GPS狀態發送改變時回調}@Overridepublic void onProviderEnabled(String provider) { //定位提供者啟動時回調}@Overridepublic void onProviderDisabled(String provider) { //定位提供者關閉時回調}});location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);//獲取最新的定位信息 }總結
以上是生活随笔為你收集整理的Android 调用原生API获取地理位置和经纬度,判断所在国家的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双面打印设置
- 下一篇: Android 视频通话