Android 獲取經緯度。

1。 添加權限 AndroidManifest.xml

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

 

 

@SuppressLint("MissingPermission")
    public void refreshLocation() {
        logger.d("refreshLocation() 000");
        ThreadExecutor.getInstance().schedule(new Runnable() {
            @Override
            public void run() {
                if (isViewClose()) { return; }
                mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                List<String> providers = mLocationManager.getProviders(true);
                if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
                    mLocationProvider = LocationManager.NETWORK_PROVIDER;
                } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
                    mLocationProvider = LocationManager.GPS_PROVIDER;
                } else {
                    if (!isGpsAble(mLocationManager)) {
                        ToastUtils.showShort("請打開GPS~");
                        openGPS2();
                    }
                    return;
                }
                Location location = mLocationManager.getLastKnownLocation(mLocationProvider);
                if (location != null) {
                    final double latitude = location.getLatitude();
                    final double longitude = location.getLongitude();
                    logger.d("refreshLocation latitude {} , longitude {} " + latitude + " longitude : " + longitude);
                    ToastUtils.showShort("經度:" + longitude + "\n緯度:" + latitude);
//                    getAddress(latitude, longitude);
                    //監聽位置變化,2秒一次,距離1米以上
                    mLocationManager.requestLocationUpdates(mLocationProvider, 2 * 1000, 100, new LocationListener() {
                        @Override
                        public void onLocationChanged(Location location) {
                            if (location != null) {
                                logger.d("refreshLocation latitude {} , longitude {} " + location.getLatitude() + " longitude : " + location.getLongitude());
                                ToastUtils.showShort("經度:" + location.getLongitude() + "\n緯度:" + location.getLatitude());
                            }
                        }

                        @Override
                        public void onStatusChanged(String provider, int status, Bundle extras) {
                            logger.d(provider + "---" + status + "===" + status + extras.toString());
                        }

                        @Override
                        public void onProviderEnabled(String provider) {
                            logger.d(provider + "---");
                        }

                        @Override
                        public void onProviderDisabled(String provider) {
                            logger.d(provider + "--777-");
                        }
                    });
                } else {

                }
            }
        }, 2000);
    }

    private boolean isGpsAble(LocationManager lm) {
        return lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? true : false;
    }

    //打開設置頁面讓用戶自己設置
    private void openGPS2() {
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivityForResult(intent, AppConstant.ActivityRequestCode.ACCOUNT_KIT_LOGIN);
    }

    public void getAddress(double latitude, double longitude) {
        if (isViewClose()) { return; }
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses != null && addresses.size() > 0) {
                Address address = addresses.get(0);
                if (address != null) {
                    String data = address.toString();
                    if (TextUtils.isEmpty(data)) {
                        LogUtils.d("getAddress()  (TextUtils.isEmpty(data)");
//                        updateUserInfo(latitude, longitude, null);
                    } else {
                        int startCity = data.indexOf("admin=") + "admin=".length();
                        int endCity = data.indexOf(",", startCity);
                        int startPlace = data.indexOf("thoroughfare=") + "thoroughfare=".length();
                        int endPlace = data.indexOf(",", startPlace);
                        int startCountry = data.indexOf("countryName=") + "countryName=".length();
                        int endCountry = data.indexOf(",", startCountry);
                        String country = data.substring(startCountry, endCountry);
                        String city = data.substring(startCity, endCity);
                        String place = data.substring(startPlace, endPlace);
//                        updateUserInfo(latitude, longitude, country + "\n" + city + "\n" + place);
                        logger.d("getAddress()" + country + "\n" + city + "\n" + place);
//                        ToastUtils.showShort("country:" + country + "\ncity:" + city + "\nplace:" + place);
                    }
                } else {
//                    updateUserInfo(latitude, longitude, null);
                }
            } else {
//                updateUserInfo(latitude, longitude, null);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            updateUserInfo(latitude, longitude, null);
        }
    }

    public boolean isViewClose() {
        return (ActivityUtil.isFinishing(this));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppConstant.ActivityRequestCode.ACCOUNT_KIT_LOGIN) {
            refreshLocation();
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章