0

Androidアプリでは、ボタンをクリックして場所を取得したいのですが、私のコードはここにあります

  public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

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

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                               LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                  .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude =    location.getLatitude();
                            longitude =      location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener Calling this function will stop using GPS in your
 * app
 * */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(GPSTracker.this);
    }
}

/**
 * Function to get latitude
 * */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude() {
    if (location != null) {
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}


/**
 * Function to check GPS/wifi enabled
 * 
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

public void onLocationChanged(Location location) {

              this.location=location;
}

public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

}

上記のコードから緯度、経度を取得する呼び出し関数。

double lat = gps.getLatitude();
double log = gps.getLongitude();

しかし、場所が変わっても同じ場所しか与えられません。

誰でも私を助けてください...前もって感謝します..

4

3 に答える 3

0

これを試して

public void onLocationChanged(Location location) {
   this.location = location;
}

また、MIN_DISTANCE_CHANGE_FOR_UPDATES を最小値に減らしてみてください。

于 2013-09-10T06:19:50.743 に答える
0

私はあなたのコードを調べましたが、それはすべて正しいようです。最小距離を与えられたので、最小距離を0にして試してみてください。距離は 10 メートルで、それほど移動していない可能性があり、サービスに時間がかかっている可能性があります。最小ではなく主要なポイントとしての距離。時間。

@Dynamo: requestupdatelocation onLocationChanged コールバック メソッドを呼び出します。場所の割り当てによっては、完全にそうではありません。場所の値は、ユーザーの場所の更新に従って変更されます。

ここでの質問は、「onLocationChanged」コールバック メソッドが特定のパラメーターで呼び出されないことです。

于 2013-09-10T06:08:05.777 に答える
0

あなたのコードは正しいようです。考えられる唯一の理由は、デバイスが場所を取得できないことです。デバイスが現在地をロックするには数分かかるため、しばらくお待ちください。

を使用adb shell dumpsys locationして、ロケーション リスナーが登録されているアプリを確認し、自分のアプリがその中にあるかどうかを確認できます。また、Android が最後に位置情報の更新を取得したのはいつかもわかります。また、他の位置情報アプリを実行して、この問題がアプリにあるのか環境にあるのかを確認してください。

于 2013-09-13T06:06:10.340 に答える