1

他の何人かの人々と私はアンドロイド用のアプリに取り組んでいます。デバイスを緯度と経度に配置する必要があります。ロケーションオブジェクトを作成できましたが、オブジェクトは常に空白です。完全に空のプロジェクトでコードを再作成しようとしましたが、それも失敗しました。これが私たちのルートアクティビティです:

package com.app.Locationtest;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

public class locationtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locman =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location loc = locman.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc==null)
        {
           finish();
        }
    }
}

マニフェストは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.Locationtest"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_GPS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".locationtest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 

この問題をどのように修正しますか?

4

2 に答える 2

1

getLastKnownLocation() javadocによると:「..プロバイダーが現在無効になっている場合は、nullが返されます。」

そのため、GPSに依存していますが、オンにはなりません。GPSを使用して他のアプリケーションに便乗するために使用されます。

于 2010-11-05T19:00:14.060 に答える
0

デバイスが場所を取得するのに時間がかかりすぎる場合があります。これは、Androidサイトにリストされている場所を取得するためのフローです。

  1. アプリケーションを起動します。
  2. しばらくしてから、目的のロケーションプロバイダーからの更新をリッスンし始めます。
  3. 新しいが精度の低い修正を除外することにより、場所の「現在の最良の見積もり」を維持します。
  4. 位置情報の更新を聞くのをやめます。
  5. 最後の最良の位置推定を利用します。

マップを表示していなくても、アプリが初期化されているため、カスタムロケーションリスナーを使用してロケーションの更新をリッスンし始めます。

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new CustomLocationListener(); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

場所をリッスンしているスレッドがあるので、ユーザーがマップビューをタップして呼び出すと、場所がnullの場合、ユーザーにメッセージを送信して、ユーザーの場所を取得するまで待機します。

最後の場所が最適な場所ではない可能性があるため、より適切な場所を選択する方法を開発することをお勧めします。これを試してください。

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
 }

このコードは、私がリンクしたのと同じページにあります。

お役に立てれば!

于 2011-11-19T13:43:25.183 に答える