0

私のアプリは、新しい現在のGPSパラメーター(3秒から8秒ごとに更新)を使用する必要があります:緯度と経度。そして私はGPSプロバイダーとネットワークプロバイダーの両方を使用しています。GPSパラメータを更新するために使用することを知っています

if(gps_enabled)
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);

if(network_enabled)
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

問題:実際、gpsは各環境の40〜50秒後に更新されます

3〜8秒後にGPSアップデートを取得するにはどうすればよいですか?

ありがとうございます

4

4 に答える 4

2
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
                try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

//lm は locationManager です

実際には。私は条件を使用しません: network_enabled または Network-provider を取得して場所を取得します。---> 動作し、新しいコード:

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, locationListenerGps);

その理由は、Network_Provider を使用していないからです。2 つの GPS とネットワーク プロバイダーの場合、システムは NEtwork_provider を使用するためです。しかし、logcat では、Smartphone が「Listenter」ループ 3 ~ 6 を Network_provider で更新していないことがわかります。逆に、GPS_PROVIDER を使用すると、スマートフォンは常に 3 ~ 6 秒で更新されます。

-- 初めて GPS を開いたとき。最初のリスナーを取得するには 30 ~ 50 秒かかります。でも大丈夫です

于 2011-05-31T22:41:04.900 に答える
2

位置、平均速度、現在の速度を返す小さなアプリを作成しました。携帯電話 (HTC Legend) で実行すると、1 秒に 1 ~ 2 回更新されます。よろしければご利用いただければ幸いです。6 つのテキストビューを含む main.xml ファイルを作成し、次の行を AndroidManifest.xml ファイルに追加するだけです。

package Hartford.gps;

import java.math.BigDecimal;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSMain extends Activity implements LocationListener {

LocationManager locationManager;
LocationListener locationListener;

//text views to display latitude and longitude
TextView latituteField;
TextView longitudeField;
TextView currentSpeedField;
TextView kmphSpeedField;
TextView avgSpeedField;
TextView avgKmphField;

//objects to store positional information
protected double lat;
protected double lon;

//objects to store values for current and average speed
protected double currentSpeed;
protected double kmphSpeed;
protected double avgSpeed;
protected double avgKmph;
protected double totalSpeed;
protected double totalKmph;

//counter that is incremented every time a new position is received, used to calculate average speed
int counter = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    run();
}

@Override
public void onResume() {
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
    super.onResume();
}

@Override
public void onPause() {
    locationManager.removeUpdates(this);
    super.onPause();
}

private void run(){

    final Criteria criteria = new Criteria();

    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setSpeedRequired(true);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    //Acquire a reference to the system Location Manager

    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    locationListener = new LocationListener() {

        public void onLocationChanged(Location newLocation) {

            counter++;

            //current speed fo the gps device
            currentSpeed = round(newLocation.getSpeed(),3,BigDecimal.ROUND_HALF_UP);
            kmphSpeed = round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);

            //all speeds added together
            totalSpeed = totalSpeed + currentSpeed;
            totalKmph = totalKmph + kmphSpeed;

            //calculates average speed
            avgSpeed = round(totalSpeed/counter,3,BigDecimal.ROUND_HALF_UP);
            avgKmph = round(totalKmph/counter,3,BigDecimal.ROUND_HALF_UP);

            //gets position
            lat = round(((double) (newLocation.getLatitude())),3,BigDecimal.ROUND_HALF_UP);
            lon = round(((double) (newLocation.getLongitude())),3,BigDecimal.ROUND_HALF_UP);

            latituteField = (TextView) findViewById(R.id.lat);
            longitudeField = (TextView) findViewById(R.id.lon);             
            currentSpeedField = (TextView) findViewById(R.id.speed);
            kmphSpeedField = (TextView) findViewById(R.id.kmph);
            avgSpeedField = (TextView) findViewById(R.id.avgspeed);
            avgKmphField = (TextView) findViewById(R.id.avgkmph);

            latituteField.setText("Current Latitude:        "+String.valueOf(lat));
            longitudeField.setText("Current Longitude:      "+String.valueOf(lon));
            currentSpeedField.setText("Current Speed (m/s):     "+String.valueOf(currentSpeed));
            kmphSpeedField.setText("Cuttent Speed (kmph):       "+String.valueOf(kmphSpeed));
            avgSpeedField.setText("Average Speed (m/s):     "+String.valueOf(avgSpeed));
            avgKmphField.setText("Average Speed (kmph):     "+String.valueOf(avgKmph));

        }

        //not entirely sure what these do yet
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}

    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);
}

//Method to round the doubles to a max of 3 decimal places
public static double round(double unrounded, int precision, int roundingMode)
{
    BigDecimal bd = new BigDecimal(unrounded);
    BigDecimal rounded = bd.setScale(precision, roundingMode);
    return rounded.doubleValue();
}


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

}

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

}

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

}

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

}

}
于 2011-06-09T16:39:06.270 に答える
0

の minTime パラメータはrequestLocationUpdates3000 ~ 8000 にする必要があります

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)

minTime the minimum通知の時間間隔 (ミリ秒単位)。このフィールドは、電力を節約するためのヒントとしてのみ使用されます。実際の場所の更新間隔は、この値よりも大きい場合も小さい場合もあります。

minDistance通知の最小距離間隔 (メートル単位)

于 2011-05-31T19:47:05.100 に答える
0

requestLocationUpdates(...)を見てください。

public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

以来: API レベル 1

指定されたプロバイダーによって定期的に通知される現在のアクティビティを登録します。定期的に、提供された LocationListener が現在の Location またはステータスの更新で呼び出されます。

最新の場所を受信するまでに時間がかかる場合があります。即時の場所が必要な場合、アプリケーションは getLastKnownLocation(String) メソッドを使用できます。

ユーザーがプロバイダーを無効にすると、更新が停止し、 onProviderDisabled(String) メソッドが呼び出されます。プロバイダーが再び有効になるとすぐに、 onProviderEnabled(String) メソッドが呼び出され、場所の更新が再開されます。

通知の頻度は、minTime および minDistance パラメータを使用して制御できます。minTime が 0 より大きい場合、LocationManager は、電力を節約するために位置更新の間に minTime ミリ秒の間休止する可能性があります。minDistance が 0 より大きい場合、デバイスが minDistance メートル移動した場合にのみ位置がブロードキャストされます。できるだけ頻繁に通知を取得するには、両方のパラメーターを 0 に設定します。

バックグラウンド サービスでは、minTime を十分に高く設定して、GPS や無線通信を常時オンにしておくことでデバイスが電力を消費しすぎないように注意する必要があります。特に、60000ms 未満の値は推奨されません。

呼び出し元のスレッドは、呼び出し元のアクティビティのメイン スレッドなどのルーパー スレッドである必要があります。

パラメーター

provider 登録するプロバイダの名前

minTime 通知の最小時間間隔 (ミリ秒単位)。このフィールドは、電力を節約するためのヒントとしてのみ使用されます。実際の場所の更新間隔は、この値よりも大きい場合も小さい場合もあります。

minDistance 通知の最小距離間隔 (メートル単位)

リスナ {#link LocationListener} で、その onLocationChanged(Location) メソッドが場所の更新ごとに呼び出されます

于 2011-05-31T19:47:49.887 に答える