0

我的应用需要使用新的当前 GPS 参数(每次 3 到 8 秒后更新):纬度和经度。我同时使用:GPS-provider 和 Network-provider。我知道用来更新 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 是位置管理器

实际上。我不使用条件:network_enabled 或 Network-provider 来获取位置。--->它工作和新代码:

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

原因,我不使用 Network_Provider。因为,当 2 GPS 和 Network Provider 时,系统会使用 NEtwork_provider。但是在 logcat 中,我看到智能手机没有使用 Network_provider 更新“Listenter”循环 3-6s。En revanche,使用 GPS_PROVIDER,智能手机更新总是 3-6 秒。

-- 第一次打开GPS;我需要 30-50 秒才能拥有第一个听众。但没关系

于 2011-05-31T22:41:04.900 回答
2

我写了一个小应用程序,它将返回位置、平均速度和当前速度。当我在手机(HTC Legend)上运行它时,它每秒更新 1-2 次。如果您愿意,我们非常欢迎您使用它。您只需要创建一个包含 6 个文本视图的 main.xml 文件,然后将此行添加到您的 AndroidManifest.xmll 文件中:

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 参数requestLocationUpdates应该是 3000 到 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 将使用当前位置或状态更新来调用。

接收最新位置可能需要一段时间。如果需要即时位置,应用程序可以使用 getLastKnownLocation(String) 方法。

如果提供者被用户禁用,更新将停止,并且 onProviderDisabled(String) 方法将被调用。再次启用提供程序后,将调用 onProviderEnabled(String) 方法并重新开始位置更新。

可以使用 minTime 和 minDistance 参数控制通知的频率。如果 minTime 大于 0,LocationManager 可能会在位置更新之间休息 minTime 毫秒以节省电力。如果 minDistance 大于 0,则仅当设备移动 minDistance 米时才会广播位置。要尽可能频繁地获取通知,请将两个参数都设置为 0。

后台服务应注意设置足够高的 minTime,以便设备不会通过始终保持 GPS 或无线无线电而消耗过多电量。特别是不建议使用低于 60000ms 的值。

调用线程必须是Looper线程,例如调用Activity的主线程。

参数

provider 要注册的提供者的名称

minTime 通知的最小时间间隔,以毫秒为单位。此字段仅用作节省电量的提示,位置更新之间的实际时间可能大于或小于此值。

minDistance 通知的最小距离间隔,以米为单位

监听一个 {#link LocationListener} ,每次位置更新都会调用其 onLocationChanged(Location) 方法

于 2011-05-31T19:47:49.887 回答