Android 开发——获取位置

Android 开发——获取位置

写作原因

因为这个部分网络上面涉及到的内容比较杂糅,所以我打算写一下,方便以后的开发学习小白少走一些弯路。

获取手机中所有可用的位置提供器

法一
LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
List<String> providerNames = locationManager.getAllProviders();
StringBuilder stringBuilder = new StringBuilder();
for (Iterator<String> iterator = providerNames.iterator(); iterator.hasNext();){
     stringBuilder.append(iterator.next()+"\n");
    }
Log.e("location",stringBuilder.toString());
法二(此处选择了GPS定位)
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationProvider locationProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER);
Log.e("Location",locationProvider.getName());
法三

此处为利用选择条件去获取你想要的位置提供器

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria,true);
Log.e("location",provider+"");

我的实现代码(仅供参考,不作解释)

onCreate内部内容
private String locationProvider;

        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // 动态请求位置信息
            Toast.makeText(this,"GPS or NETWORK error",Toast.LENGTH_SHORT).show();
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION}, 1);
            return;
        }
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            locationProvider = LocationManager.GPS_PROVIDER;
            Toast.makeText(this,"GPS",Toast.LENGTH_SHORT).show();
        }
        else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            locationProvider = LocationManager.NETWORK_PROVIDER;
            Toast.makeText(this,"NETWORK",Toast.LENGTH_SHORT).show();
        }
        else if (providers.contains(LocationManager.PASSIVE_PROVIDER)){
            locationProvider = LocationManager.PASSIVE_PROVIDER;
            Toast.makeText(this,"PASSIVE",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(this,"请开启位置请求权限",Toast.LENGTH_SHORT).show();
            return;
        }

        locationManager.requestLocationUpdates(
                locationProvider,
                1000,
                1,
                new LocationListener() {
                    @Override
                    public void onLocationChanged(@NonNull Location location) {

                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        LocationListener.super.onStatusChanged(provider, status, extras);
                    }

                    @Override
                    public void onProviderEnabled(@NonNull String provider) {
                        LocationListener.super.onProviderEnabled(provider);
                    }

                    @Override
                    public void onProviderDisabled(@NonNull String provider) {
                        LocationListener.super.onProviderDisabled(provider);
                    }
                }
        );
//        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Location location = locationManager.getLastKnownLocation(locationProvider);
        locationUpdates(location);
locationUpdates函数
    public void locationUpdates(Location location){
        if (location!=null){
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("location:\n");
            stringBuilder.append("经度:");
            stringBuilder.append(location.getLongitude());
            stringBuilder.append("纬度:");
            stringBuilder.append(location.getLatitude());
            Toast.makeText(ShowActivity.this,stringBuilder.toString(),Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(ShowActivity.this,"error",Toast.LENGTH_LONG).show();
        }
    }

你可能感兴趣的:(Android,android,java)