如何使用GPS提供商获得当前准确的位置?

时间:2018-01-15 08:12:43

标签: android gps location currentlocation

我在GPS提供商的帮助下需要用户的当前位置。 目前我正在获取GPS位置,但在某些设备中我没有获得位置。

我还尝试过上次知道的位置和网络位置,但它们并不准确。

所以请提供更好的解决方案。

我目前的代码:

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
MapsInitializer.initialize(getActivity());
LocationListener listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            final double latitude = location.getLatitude();
            final double longitude = location.getLongitude();
            if (getActivity() == null){
                return;
            }
            Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
            try {
                List<Address> fromLocation = geocoder.getFromLocation(latitude, longitude, 1);
                Address address = null;
                if (fromLocation != null && fromLocation.size() > 0) {
                    address = fromLocation.get(0);
                    final String locality = address.getLocality();
                    if (dataSend != null) {
                        dataSend.onDataSend(address);
                    }

                    fragmentMapBinding.map.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap googleMap) {
                            LatLng latLng = new LatLng(latitude, longitude);
                            googleMap.clear();
                            CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 19, 0, 0));
                            googleMap.animateCamera(cameraUpdate);
                            googleMap.getUiSettings().setZoomGesturesEnabled(false);
                            googleMap.getUiSettings().setScrollGesturesEnabled(false);
                            googleMap.addMarker(new MarkerOptions().position(latLng).title("Your Location"));
                        }
                    });
                }


            } catch (IOException e) {

                Log.e(TAG, "getMap", e);
            }

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            Toast.makeText(getActivity(), s + " : " + i, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String s) {
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderDisabled(String s) {
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
        }
    };
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

0 个答案:

没有答案