Android如何获取GPS位置onLocationChanged的当前速度和方向?

时间:2011-09-05 12:52:03

标签: android

有办法吗? 因为getSpeed和getBearing总是返回0.0。

如何才能获得正确的速度并获得正确的速度?

当前代码是103 up:

What is the simplest and most robust way to get the user's current location on Android?

我的代码是:

public class MyLocation {
    Timer timer1;
    Timer timer2;

    LocationManager lm;
    LocationResult locationResult;

    boolean gps_enabled=false;
    boolean network_enabled=false;

    Location last_loc;

    public boolean getLocation(Context context, LocationResult result)  {

        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //új
        Criteria criteria = new Criteria();
        criteria.setSpeedRequired(true);
        criteria.setBearingRequired(true);
        List<String> providers = lm.getProviders(criteria, false);

        if (providers == null || providers.size()== 0) {
         //Sorry it doesn't do it by default.
            Log.e("providers", "providers null");
        }


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

        if(!gps_enabled && !network_enabled)
            return false;

        if (gps_enabled) {
            lm.addGpsStatusListener(gpsstatusListenerGps);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        }
        if (network_enabled && !gps_enabled) {
            lm.addGpsStatusListener(gpsstatusListenerGps);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        }


        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), 20000);
        return true;
    }


    public boolean getSatellites(Context context, LocationResult result)  {

        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if (lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //új
        Criteria criteria = new Criteria();
        criteria.setSpeedRequired(true);
        criteria.setBearingRequired(true);
        List<String> providers = lm.getProviders(criteria, false);

        if (providers == null || providers.size()== 0) {
         //Sorry it doesn't do it by default.
            Log.e("providers", "providers null");
        }


        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if (gps_enabled) {
            lm.addGpsStatusListener(gpsstatusListenerGps);
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        }
        if (network_enabled && !gps_enabled) {
            lm.addGpsStatusListener(gpsstatusListenerGps);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        }
        timer2=new Timer();
        timer2.schedule(new GetLastGprsStatus(), 20000);
        return true;
    }

    public LocationManager getLocationManager() {
        return lm;
    }

    android.location.GpsStatus.Listener gpsstatusListenerGps = new android.location.GpsStatus.Listener() {
        public void onGpsStatusChanged(int event) {

            timer2.cancel();

            switch (event) {
                case GpsStatus.GPS_EVENT_STARTED:
                    break;
                case GpsStatus.GPS_EVENT_STOPPED:
                    break;
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    GpsStatus gpsStatus             = lm.getGpsStatus(null);
                    locationResult.gotSatellites(gpsStatus);
                    break;
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    GpsStatus gpsStatus0            = lm.getGpsStatus(null);
                    locationResult.gotSatellites(gpsStatus0);
                    break;
            }

            lm.removeGpsStatusListener(gpsstatusListenerGps);
        }
    };

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);

            //lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            //lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {

             //Location

             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if (gps_enabled) {
                 gps_loc    =lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                 last_loc   =gps_loc;
             }
             if (network_enabled && !gps_enabled) {
                 net_loc    =lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                 last_loc   =net_loc;
             }

             //if there are both values use the latest one
             if (gps_loc!=null && net_loc!=null) {
                 if (gps_enabled) 
                     locationResult.gotLocation(gps_loc);
                 else if (network_enabled && !gps_enabled)
                     locationResult.gotLocation(net_loc);

                 return;
             }

             if (gps_loc!=null) {
                 locationResult.gotLocation(gps_loc);
                 return;
             }
             if (net_loc!=null) {
                 locationResult.gotLocation(net_loc);
                 return;
             }
             if ((gps_loc==null) && net_loc==null) locationResult.gotLocation(null);

        }
    }

    class GetLastGprsStatus extends TimerTask {
        @Override
        public void run() {
             lm.removeGpsStatusListener(gpsstatusListenerGps);

             //Location net_loc=null, gps_loc=null;
             GpsStatus gpsStatus=null;

             if (last_loc!=null){
                 gpsStatus= lm.getGpsStatus(null);
                 locationResult.gotSatellites(gpsStatus);
                 return;
             } else locationResult.gotSatellites(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
        public abstract void gotSatellites(GpsStatus gpsStatus);
    }


}

...

static com.my.balloon.MyLocation myLocation = new com.my.balloon.MyLocation();
public static Location myLoc;

public static void getYourLocation() {
   myLocation.getLocation(mycontext, locationResult);
   myLocation.getSatellites(mycontext, locationResult);
}


public static LocationResult locationResult = new LocationResult() {
    @Override
    public void gotLocation(final Location location){
        myLoc=location;

        //30 sec
        if (System.currentTimeMillis()-lastLocationAddedTime>timerAddGPSRefresh)    {
            yourGPSLocations.add(myLoc);

            lastLocationAddedTime=System.currentTimeMillis();

            Utils.hint(mycontext, "myloc ADDED "+Utils.getDatetimeFromUTCTimeN(myLoc.getTime()));
        }

        Log.e("myloc", "getting myloc "+Utils.getDatetimeFromUTCTimeN(myLoc.getTime())+" - dir "+myLoc.getBearing());
        Utils.hint(mycontext, "myloc "+Utils.getDatetimeFromUTCTimeN(myLoc.getTime())+" - dir "+myLoc.getBearing());
    }

    @Override
    public void gotSatellites(GpsStatus gpsStatus) {
        xx.gpsStatus=gpsStatus;
        Log.e("gpsStatus", "getting gpsStatus "+gpsStatus);
        Utils.hint(mycontext, "gpsStatus");
    }
};

定时器:

    timerTime = new Timer();
    timerTime.schedule(new TimerTask() {
        @Override  public void run() {  
            mHandler.post(new Runnable() {
                @Override public void run() {
                                        getYourLocation();
                }
            });
        }
    }, 100, 1000*4);

0 个答案:

没有答案