Guida allo sviluppo

Local Navigation

Esempio di codice: modifica dei criteri per il recupero delle informazioni di posizione

import net.rim.device.api.gps.GPSInfo;
import javax.microedition.location.*;
public class handleGPS
{
    static LocationProvider locationProvider;
    static double lat, lon;
    static float  alt, spd, crs;
    public static void main(String[] args)
    {
    }
    public handleGPS(int gpsMode)
    {
        if (locationProvider != null)
        {
            locationProvider.reset();
            locationProvider.setLocationListener(null, -1, -1, -1);
        }
        Criteria myCriteria = new Criteria();
        myCriteria.setPreferredResponseTime(Criteria.NO_REQUIREMENT);
        myCriteria.setCostAllowed(true);
        if ( gpsMode == GPSInfo.GPS_MODE_AUTONOMOUS )
        {
            myCriteria.setCostAllowed(false);
        }
        else if ( gpsMode == GPSInfo.GPS_MODE_ASSIST )
        {
            myCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_MEDIUM);
        }
        else
        {
            myCriteria.setPreferredPowerConsumption(Criteria.POWER_USAGE_LOW);
        }
        try
        {
            locationProvider = LocationProvider.getInstance(myCriteria);
            if (locationProvider != null)
            {
                locationProvider.setLocationListener
                    (new myLocationListener(), -1, -1, -1);
            }
        }
        catch (Exception err)
        {
        }
    }
    private static class myLocationListener implements LocationListener
    {
        public void locationUpdated(LocationProvider provider, Location location)
        {
            lat = location.getQualifiedCoordinates().getLatitude();
            lon = location.getQualifiedCoordinates().getLongitude();
            alt = location.getQualifiedCoordinates().getAltitude();
            spd = location.getSpeed();
            crs = location.getCourse();
        }
        public void providerStateChanged(LocationProvider provider, int newState)
        {
        }
    }
}
Argomento successivo: Gestione degli errori

Le informazioni sono state utili? Inviateci i vostri commenti.