0

私は現在、ユーザーから最も近い場所を取得し、リスト ビューで並べ替える必要があるプロジェクトに取り組んでいます。

このステップに到達するまで、すべてが順調に進んでいます。ここで 1 週間立ち往生しており、解決方法がわかりません。

プログラムが開始されたときの経度と緯度を取得したい。私はそれにフラグメントを使用しています。ユーザーがフラグメントを開くと、現在のユーザーの位置から緯度と経度が自動的に取得されます。

ここでは、サービスを使用して GPS の緯度と経度を取得します。しかし、それはまったく機能しません。緯度と経度は常に 0 の値で返されます。

これを解決する方法を教えてください。ありがとう。

NB これは私の GPSTracker.java コードです

public class GPSTrackStandAlone extends Service implements LocationListener {
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private Context mContext;
private boolean canGetLocation = false;
private Location mLocation;
private double mLatitude;
private double mLongitude;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 60000;
private LocationManager mLocationManager;
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;

public GPSTrackStandAlone(Context mContext) {

    this.mContext = mContext;

}


public Location getLocation() {
    try {
        LocationManager mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

        /*getting status of the gps*/
        Boolean isGpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        /*getting status of network provider*/
        Boolean isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGpsEnabled && !isNetworkEnabled) {

            /*no location provider enabled*/
        } else {

            this.canGetLocation = true;

            /*getting location from network provider*/
            if (isNetworkEnabled) {
                if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                        || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    mLocationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_FOR_UPDATE,
                            MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
                    Log.d("Perms", "Permission for GPS Granted");

                    if (mLocationManager != null) {

                        mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (mLocation != null) {
                            mLatitude = mLocation.getLatitude();
                            mLongitude = mLocation.getLongitude();
                        }
                    }
                }
                /*if gps is enabled then get location using gps*/
                if (isGpsEnabled) {
                    if (mLocation == null) {
                        mLocationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_FOR_UPDATE,
                                MIN_DISTANCE_CHANGE_FOR_UPDATE, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (mLocationManager != null) {
                            mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (mLocation != null) {
                                mLatitude = mLocation.getLatitude();
                                mLongitude = mLocation.getLongitude();
                            }

                        }
                    }

                }
            }
        }

    } catch (Exception e) {

        e.printStackTrace();
    }

    return mLocation;
}

/**
 * call this function to stop using gps in your application
 */
public void stopUsingGps() {

    if (mLocationManager != null) {
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            mLocationManager.removeUpdates(GPSTrackStandAlone.this);
        }

    }
}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        Toast.makeText(GPSTrackStandAlone.this, "isGooglePlayServiceAvailable = False", Toast.LENGTH_SHORT).show();
        return false;
    }
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}



public double getLatitude() {
    if (mLocation != null) {

        mLongitude = mLocation.getLongitude();
        Log.d("MGPS", String.valueOf(mLocation.getLatitude()));
    }
    return mLongitude;
}

public double getLongitude() {
    Log.d("function", "getLongitude at GPSTrack");

    if (mLocation != null) {

        mLongitude = mLocation.getLongitude();
        Log.d("MGPS", String.valueOf(mLocation.getLatitude()));
    }

    return mLongitude;
}


public boolean canGetLocation() {

    return this.canGetLocation;
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

4

0 に答える 0