【问题标题】:force update many times :Gps Location during 3-6 seconds多次强制更新:Gps 位置在 3-6 秒内
【发布时间】:2011-06-10 00:39:06
【问题描述】:

我的应用需要使用新的当前 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 更新??

谢谢你

【问题讨论】:

    标签: android gps locationmanager locationlistener


    【解决方案1】:
    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 时,系统会使用 NEtwork_provider。但是在 logcat 中,我看到智能手机没有使用 Network_provider 更新“Listenter”循环 3-6s。 En revanche,使用 GPS_PROVIDER,智能手机更新总是 3-6 秒。

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

    【讨论】:

      【解决方案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
      
      }
      
      }
      

      【讨论】:

        【解决方案3】:

        requestLocationUpdates的minTime参数应该是3000到8000

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

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

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

        【讨论】:

        • 嗨 Reno 事实上,我确实设置了 minTime:3000 或 8000;但它没有用;理论上,requestLocationUpdates(provider,0,0,listener) 会更频繁地通知。但它仍然是 40-50 更新。谢谢
        • 你是对的;在我删除条件后:gps_enable 和 network_enable,它工作
        • 是的,第一次修复需要一些时间。可以从 40 秒到 10 分钟。
        【解决方案4】:

        看看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的主线程。

        参数

        提供要注册的提供者的名称

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

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

        监听一个{#link LocationListener},它的onLocationChanged(Location)方法会在每次位置更新时被调用

        【讨论】:

        • 感谢 Aleadam,但我仍然找不到解决方案。谷歌地图或导航,他们可以在间隔 2-4 秒或几米后更新位置。因此,我们有办法解决吗?
        • @maydenec 你要搬家吗?除非位置发生变化,否则您不会收到任何位置更新。
        • 抱歉,我刚刚用简单的 GPS 定位出错了。它工作,
        猜你喜欢
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多