使gps调用更快更有效

时间:2013-08-09 20:18:10

标签: android performance gps

我最近一直在为我正在处理的应用程序进行简单的速度计算,但是我的代码需要很长时间来检索位置,我知道之前已经问过这样的问题,但没有答案似乎找回了我正在寻找的结果。那么,我怎样才能让这段代码在几秒钟内完成gps修复,甚至可能呢? 我的LocationListener:

package me.dylan.acf;

import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.NotificationManager;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.TextView;

public class GPSManager implements LocationListener {
    ArrayList<Double> avgspeeds = new ArrayList<Double>();
    TextView debug;
    NotificationManager mngr;
    double avgspeed;
    long lastTime = 0;
    GraphView view;
    Location lastloc;
    int earthRadius = 6371;
    long delaytime = 30;
    ArrayList<Double> allspeeds = new ArrayList<Double>();

    public GPSManager(TextView view) {
        debug = view;
        Location location = ACF.instance.lmanager
                .getLastKnownLocation(ACF.instance
                        .getProperLocationsServices(ACF.instance
                                .getApplicationContext()));
        if (location != null) {
            double speed = location.getSpeed();
            lastloc = location;

            debug.setText("Average Speed: " + avgspeed + "\nCurrent speed: "
                    + speed + "\nLocation updates: " + avgspeeds.size());
        }
    }

    @Override
    public void onLocationChanged(Location location) {
//      DecimalFormat format = new DecimalFormat("0.00");
        double speed = location.getSpeed();
         if (lastloc != null) {
         double latDist = Math.toRadians(location.getLatitude()
         - lastloc.getLatitude());
         double lonDist = Math.toRadians(location.getLongitude()
         - lastloc.getLongitude());
         double lat1 = Math.toRadians(location.getLatitude());
         double lat2 = Math.toRadians(lastloc.getLatitude());
         double a = Math.sin(latDist / 2) * Math.sin(latDist / 2)
         + Math.sin(lonDist / 2) * Math.sin(lonDist / 2)
         * Math.cos(lat1) * Math.cos(lat2);
         double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
         double dist = earthRadius * c;
         speed = (dist * 0.621371) / Math.abs(System.currentTimeMillis() - lastTime * 60 * 60 * 60);
         lastTime = System.currentTimeMillis();
         }
        allspeeds.add(speed);

        if (allspeeds.size() > 30) {
            allspeeds.remove(0);
        }
        avgspeed = 0;
        for (double d : allspeeds) {
            avgspeed += d;
        }
        avgspeed /= allspeeds.size();
        // avgspeed = Double.parseDouble(format.format(avgspeed));
        avgspeeds.add(avgspeed);
        lastloc = location;
        debug.setText("Average Speed: " + avgspeed + "\nCurrent speed: "
                + speed + "\nLocation updates: " + avgspeeds.size());

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @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

    }

}

我称之为:

public void updateWithProperService() {
        lmanager.requestSingleUpdate(
                getProperLocationsServices(getApplicationContext()), GPSmngr,
                null);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                updateWithProperService();
            }
        }, 10000);
    }

    public String getProperLocationsServices(Context context) {
        if (lmanager == null)
            lmanager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
        int minTime = 3000;
        /*
         * boolean isGPS = false; boolean isNetwork = false; try { isGPS =
         * lmanager.isProviderEnabled(LocationManager.GPS_PROVIDER); } catch
         * (Exception e) { e.printStackTrace(); } try { isNetwork = lmanager
         * .isProviderEnabled(LocationManager.NETWORK_PROVIDER); } catch
         * (Exception e) { e.printStackTrace(); }
         */
        List<String> matchingProviders = lmanager.getAllProviders();

        Location bestResult = null;
        long bestTime = 0;
        for (String provider : matchingProviders) {
            Location location = lmanager.getLastKnownLocation(provider);
            if (location != null) {
//              float accuracy = location.getAccuracy();
                long time = location.getTime();

                // float bestAccuracy;
                /*
                 * if ((time > minTime && accuracy < bestAccuracy )) {
                 * bestResult = location; bestTime = time; } else
                 */if (time < minTime &&
                /* bestAccuracy == Float.MAX_VALUE && */time < bestTime) {
                    bestResult = location;
                    bestTime = time;
                }
            }
        }
        if (bestResult != null)
            return bestResult.getProvider();
        else
            return LocationManager.NETWORK_PROVIDER;
    }

2 个答案:

答案 0 :(得分:1)

谷歌在最近的Google IO 2013活动中发布了一个不错的API:

https://developers.google.com/events/io/sessions/324498944

您应该查看它,看看如何最小化您的代码。

请注意,它需要设备才能使用Play商店应用程序。

与使用普通位置传感器(电池,速度,精度等)相比,这种方法有许多优点。

答案 1 :(得分:0)

如果在使用辅助GPS之前和之后禁用gps,则需要大约15秒。 你无法改善这一点。

Whitout AGPS在良好的条件下需要25-35秒。 该术语称为“首次修复时间”(TTF)。