getLastKnownLocation(LocationManager.NETWORK_PROVIDER)はlocation(1.0、1.0)を返します

チェン

LocationManager APIを使用して、ネットワークごとに現在の場所を取得したいと思います。

私のコード:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (isNetworkEnabled) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this);
    Log.d("Network", "Network Enabled");

    if (locationManager != null) {
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
        }
    }
}

問題は、取得した緯度と経度の両方が1.0であるということです。

W4R10CK

Googleは用途のユーザーのマッピングしlastKnownLocationた場合は、場所のためにlastknownLocation、それは別のプロバイダの発言を通じて場所を取得するために時間がかかる不明ですGPSNetworklastKnownLocationをベースロケーションとして使用し、LocationListenerで更新することをお勧めします。

       if (ActivityCompat.checkSelfPermission
                (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                &&
                ActivityCompat.checkSelfPermission
                        (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }, 1);

        }
        else {

            // enable location buttons
            googleMap.setMyLocationEnabled(true);
            googleMap.getUiSettings().setMyLocationButtonEnabled(true);

            // fetch last location if any from provider - GPS.
            final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            final Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            //if last known location is not available
            if (loc == null) {

                final LocationListener locationListener = new LocationListener() {
                    @Override
                    public void onLocationChanged(final Location location) {

                        // getting location of user
                        final double latitude = location.getLatitude();
                        final double longitude = location.getLongitude();
                        //do something with Lat and Lng
                    }

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

                    @Override
                    public void onProviderEnabled(String provider) {
                        //when user enables the GPS setting, this method is triggered.
                    }

                    @Override
                    public void onProviderDisabled(String provider) {
                        //when no provider is available in this case GPS provider, trigger your gpsDialog here.
                    }
                };

                //update location every 10sec in 500m radius with both provider GPS and Network.

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000, 500, locationListener);
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, locationListener);
            }
            else {
                //do something with last known location.
                // getting location of user
                final double latitude = loc.getLatitude();
                final double longitude = loc.getLongitude();
            }
        }

許可の処理:

     @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {

        case 1:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED){
                //do something
            }
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

locationManager.getLastKnownLocation()はnullを返します

分類Dev

LocationManagerは、AndroidのgetLastKnownLocationでnull経度を返します

分類Dev

Androidで0,0を返すgetLastKnownLocation

分類Dev

((unsigned int)0-1)> 0の場合はtrueを返します

分類Dev

pow(1,0)は0を返しますか?

分類Dev

CountAは0を返し、WorksheetFunction.CountAは1を返します

分類Dev

階乗の場合は1を返し0を返します

分類Dev

rand()/ RAND_MAXは[0、1)または[0,1]を返しますか?

分類Dev

なぜ( "0" + "1")。toInt()!0ではなく1をIntとして返します

分類Dev

WEXITSTATUS(childStatus)は0を返しますが、waitpidは-1を返します

分類Dev

Laravel DB :: updateは0または1のみを返します

分類Dev

SQLScope_Identityが0または-1を返しています

分類Dev

関数は1を返す必要がありますが、0を返します

分類Dev

locationManager.locationはnilを返します

分類Dev

getLastKnownLocationはnullを返します

分類Dev

getLastKnownLocationはNull値を返します

分類Dev

(int)Math.Pow(10、0)は1ではなく0を返します

分類Dev

MySql COUNT(*)は0ではなく1を返します

分類Dev

tf.onesは1ではなく0を返します

分類Dev

Cでは、!0は常に1を返しますか?

分類Dev

Javascript ""。lengthは0ではなく1を返します

分類Dev

request.getRemoteAddr()を使用すると0:0:0:0:0:0:0:1が返されます

分類Dev

a [0]が1を返し、a [1]が0を返すのはなぜですか?

分類Dev

scandirは[0] =>を返します。および[1] => ..配列内

分類Dev

Android getMaxAddressLineIndexは、1行目で0を返します

分類Dev

rusqliteはErr(InvalidParameterCount(1、0))を返します

分類Dev

0と1以外の場合は、falseを返します

分類Dev

OpenCV matchTemplate minVal maxVal は 0 と 1 のみを返します

分類Dev

Group ByのCount(*)-「0」ではなく1を返します

Related 関連記事

  1. 1

    locationManager.getLastKnownLocation()はnullを返します

  2. 2

    LocationManagerは、AndroidのgetLastKnownLocationでnull経度を返します

  3. 3

    Androidで0,0を返すgetLastKnownLocation

  4. 4

    ((unsigned int)0-1)> 0の場合はtrueを返します

  5. 5

    pow(1,0)は0を返しますか?

  6. 6

    CountAは0を返し、WorksheetFunction.CountAは1を返します

  7. 7

    階乗の場合は1を返し0を返します

  8. 8

    rand()/ RAND_MAXは[0、1)または[0,1]を返しますか?

  9. 9

    なぜ( "0" + "1")。toInt()!0ではなく1をIntとして返します

  10. 10

    WEXITSTATUS(childStatus)は0を返しますが、waitpidは-1を返します

  11. 11

    Laravel DB :: updateは0または1のみを返します

  12. 12

    SQLScope_Identityが0または-1を返しています

  13. 13

    関数は1を返す必要がありますが、0を返します

  14. 14

    locationManager.locationはnilを返します

  15. 15

    getLastKnownLocationはnullを返します

  16. 16

    getLastKnownLocationはNull値を返します

  17. 17

    (int)Math.Pow(10、0)は1ではなく0を返します

  18. 18

    MySql COUNT(*)は0ではなく1を返します

  19. 19

    tf.onesは1ではなく0を返します

  20. 20

    Cでは、!0は常に1を返しますか?

  21. 21

    Javascript ""。lengthは0ではなく1を返します

  22. 22

    request.getRemoteAddr()を使用すると0:0:0:0:0:0:0:1が返されます

  23. 23

    a [0]が1を返し、a [1]が0を返すのはなぜですか?

  24. 24

    scandirは[0] =>を返します。および[1] => ..配列内

  25. 25

    Android getMaxAddressLineIndexは、1行目で0を返します

  26. 26

    rusqliteはErr(InvalidParameterCount(1、0))を返します

  27. 27

    0と1以外の場合は、falseを返します

  28. 28

    OpenCV matchTemplate minVal maxVal は 0 と 1 のみを返します

  29. 29

    Group ByのCount(*)-「0」ではなく1を返します

ホットタグ

アーカイブ