位置Menager - 位置更新

时间:2015-06-04 18:29:09

标签: java android parse-platform geolocation location

所以我试图使用locationMenager每5秒更新一次用户的当前位置,但它无法正常工作。这里的代码: GPSTracker.java

public class GPSTracker extends Service implements LocationListener {


private final Context context;

boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;

Location location;

double latitude;
double longitude;

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 5000;

protected LocationManager locationManager;

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

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

        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if(!isGPSEnabled && !isNetworkEnabled) {

        } else {
            this.canGetLocation = true;

            if (isNetworkEnabled) {

                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                    if (location != null) {

                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

            }

            if(isGPSEnabled) {
                if(location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    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;
}


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

public double getLatitude() {
    if(location != null) {
        latitude = location.getLatitude();
    }
    return latitude;
}

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

    return longitude;
}

public boolean canGetLocation() {
    return this.canGetLocation;
}

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

    alertDialog.setTitle("GPS is settings");

    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            context.startActivity(intent);
        }
    });

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

如您所见,它已设置为更新。这是SearchActivity.java中的onCreate()

 gps = new GPSTracker(SearchActivity.this);

    if (gps.canGetLocation()) {


        ParseUser user2 = ParseUser.getCurrentUser();
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        geoPoint = new ParseGeoPoint(latitude, longitude);
        user2.put("lat_long", geoPoint);
        textView.setText(latitude+"  "+longitude);
        user2.saveInBackground();


    } else {
        gps.showSettingsAlert();
    }

因此,如果我将上面的代码放在onClick方法中,它会工作,它会更新当前位置。我试图用TimerTask实现它,但位置最终为(0,0)。那么有谁知道我能在这做什么?因为最好我想为该位置制作一个On / Off开关,如果可能的话,让它在后台运行,但首先我想解决位置更新的问题。

1 个答案:

答案 0 :(得分:0)

  @Override
public void onLocationChanged(Location location) {
    this.location = location;
    getLatitude();
    getLongitude();

    ParseUser user2 = ParseUser.getCurrentUser();
    ParseGeoPoint geoPoint = new ParseGeoPoint(getLatitude(), getLongitude());
    user2.put("lat_long", geoPoint);
    user2.saveInBackground();




}

好的,这就是答案,只需更改onLocationChanged。