OnLocationChanged callback is never called

前端 未结 14 1412
星月不相逢
星月不相逢 2020-12-01 03:28

I am trying to get the users current location using the LocationManager. I have done a lot of research and can\'t seem to find anyone with the same problem. T

14条回答
  •  眼角桃花
    2020-12-01 03:44

    Replacing

    LocationManager.GPS_PROVIDER

    with

    LocationManager.NETWORK_PROVIDER

    solved my problem.

    Here is code snippet of my location manager

     LocationManager locationManager = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE);
    

    //Check for Location permissions, Marshmallow and above

            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
    
                Toast.makeText(getActivity(), "Not Enough Permission", Toast.LENGTH_SHORT).show();
                return;
            }
    

    //Get current location to start with

            Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    
            currentLatitude = myLocation.getLatitude();
            currentLongitude = myLocation.getLongitude();
    

    // Request location update using LocationManager.NETWORK_PROVIDER

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES, MapFragment.this);
    

提交回复
热议问题