栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用Android 原生 API获取经纬度并且根据经纬度解析出当前具体位置信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Android 原生 API获取经纬度并且根据经纬度解析出当前具体位置信息

Google  developer url: https://developer.android.com/training/location/permissions

Demo GitHub: https://github.com/JasonZhangHG/LocationUtils.git

1.动态获取经纬度权限

2.获取经纬度:根据 LocationManager  NETWORK_PROVIDERGPS_PROVIDERPASSIVE_PROVIDER 去获取

  public static void getLocation(final ICallback callback) {
        if (ActivityCompat.checkSelfPermission(CCApplication.getInstance(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(CCApplication.getInstance(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            if (callback != null) {
                callback.onError(new Throwable("no permission"));
            }
            return;
        }
        String mLocationProvider;
        final LocationManager locationManager = (LocationManager) CCApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            if (callback != null) {
                callback.onError(new Throwable("locationManager null"));
            }
            return;
        }
        List providers = locationManager.getProviders(true);
        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
//                updateLocation(location, callback);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        if (providers == null || providers.isEmpty()) {
            if (callback != null) {
                callback.onError(new Throwable("providers null"));
            }
            return;
        }
        if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, null);
            mLocationProvider = LocationManager.NETWORK_PROVIDER;
        } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, null);
            mLocationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.PASSIVE_PROVIDER)) {
            locationManager.requestSingleUpdate(LocationManager.PASSIVE_PROVIDER, locationListener, null);
            mLocationProvider = LocationManager.PASSIVE_PROVIDER;
        } else {
            if (callback != null) {
                callback.onError(new Throwable("providers contains null"));
            }
            return;
        }
        Location location = locationManager.getLastKnownLocation(mLocationProvider);
        updateLocation(location, callback);
    }

    public static void updateLocation(Location location, final ICallback callback) {
        if (location == null) {
            if (callback != null) {
                callback.onError(new Throwable("location null"));
            }
            return;
        }
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        Log.i(TAG, "latitude : " + latitude + "  longitude : " + longitude);
        getAddress(latitude, longitude, callback);
    }

3.根据经纬度去解析具体位置:主要是Geocoder-->getFromLocation

  public static void getAddress(double latitude, double longitude, final ICallback callback) {
        Geocoder geocoder = new Geocoder(CCApplication.getInstance(), Locale.ENGLISH);
        try {
            List addresses = geocoder.getFromLocation(latitude, longitude, 1);
            Address address = addresses != null && addresses.size() > 0 ? addresses.get(0) : null;
            if (address != null) {
                String countryName = address.getCountryName();
                String countryCode = address.getCountryCode();
                Log.i(TAG, "countryName : " + countryName + "  countryCode : " + countryCode);
                if (callback != null) {
                    callback.onResult(new LocationBean(latitude, longitude, countryName, countryCode, address.toString()));
                }
            } else {
                if (callback != null) {
                    callback.onError(new Throwable("address null"));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (callback != null) {
                callback.onError(e);
            }
        }
    }

4.最终结果: 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821242.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号