如何在屏幕上更新GPS

时间:2013-12-02 15:27:54

标签: android gps android-studio

我按照一个教程,点击一个按钮,它调用一个带有所有GPS纬度/长信息的类,事情是我需要它自动调用,每个特定的时间和设备的位移它必须显示屏幕上的新值,但它只显示第一次。这就是我所拥有的:

public class GPS extends Service implements LocationListener{

private final Context mContext;

//flag for GPS status
boolean isGPSEnable = false;

// Flag for network status
boolean isNetworkEnable = false;

// flag for GPS status
boolean canGetLocation = false;

Location location;  //location
double latitude;     //latitude
double longitude;   //longitude

// the minimum distances to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // Minimun distance 10 meters

// The minimum time between updates in millisenconds
private static final long MIN_TIME_BTW_UPDATES = 1000 * 60 * 1; // 1 MINUTE

// Declaring a Location Manager
protected LocationManager locationManager;

public GPS(Context context){
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(Context.LOCATION_SERVICE);

        //getting GPS status
    isGPSEnable = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);

        //getting network status
        isNetworkEnable = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnable && !isNetworkEnable){
            // no network provider is enable
        }
        else {
            this.canGetLocation = true;
            if(isNetworkEnable){
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BTW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
                Log.d("Network", "Network");
                if(locationManager != null){
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if(location != null){
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enable get lat/long using GPS Services
            if(isGPSEnable){
                if(location == null){
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BTW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS ENABLE", "GPS Enabled");
                    if(locationManager != null){
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null){
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    }catch (Exception e){
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 */

public void stopUsingGPS(){
    if (locationManager != null){
        locationManager.removeUpdates((LocationListener) GPS.this);
    }
}

/**
 * function to get latitude
 */
public double getLatitude(){
        if(location != null){
            latitude = location.getLatitude();
        }
    //return latitude
    return latitude;
}

public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }
    //return longitude
    return  longitude;
}

/**
 * Function to check GPS/wifi enable
 * @return boolean
 */
public  boolean canGetLocation(){
    return this.canGetLocation;
}

/**
 * Function to show settings alert dialog
 * On pressing Settings button will launch Settings Options
 */

public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle(mContext.getString(R.string.AlertDialog_Tittle));

    // Setting Dialog Message
    alertDialog.setMessage(mContext.getString(R.string.dialog_message));

    // On pressing Setting button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        }
    });

    // On pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
}

@Override
public void onLocationChanged(Location location){
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

主要活动

public class GPSActivity extends ActionBarActivity {

private EditText edTLatitud;
private EditText edTLongitud;
private EditText edTCompass;
private EditText edTDirecc;
private SensorManager sensorManager;
private Sensor compassSensor;

// GPS class
GPS gps;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gpsactivity);

    edTCompass = (EditText)this.findViewById(R.id.edTxtBrujula);
    edTDirecc = (EditText)this.findViewById(R.id.edTxtBrujdireccion);
    edTLatitud = (EditText)this.findViewById(R.id.edTxtLatitud);
    edTLongitud = (EditText)this.findViewById(R.id.edTxtLongitud);

    gps = new GPS(GPSActivity.this);

    // Check if GPS is enable
    if(gps.canGetLocation()){

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        // Print on the screen the coordinates
        UpdateGPSonScreen(latitude, longitude);
    }
    else {
        // can't get location
        // GPS or network is not enable
        // Ask user to enable GPS/network in settings
        gps.showSettingsAlert();
    }
}

感谢您的建议

1 个答案:

答案 0 :(得分:0)

您只拨打UpdateGPSonScreen一次。如果你需要不断更新,你每次都会在onLocationChanged(Location location)内调用该方法,但为了让你这样做,你需要重构一下。