问题
I'm trying to develop application that uses GPS locations for services. Each service should has a location (GeoPoint). I'm trying to get my current location and save it to parse database. I tried to use getCurrentLocationInBackground but unfortunately there is not guide on how to use this. I used the below code but unfortunately it't not working. Please help!
void getLocation() {
// ParseGeoPoint location = null;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
ParseGeoPoint.getCurrentLocationInBackground(20000, criteria, new LocationCallback() {
@Override
public void done(ParseGeoPoint geoPoint, ParseException e) {
if (e == null) {
Toast.makeText(getApplicationContext(), geoPoint.getLatitude()+"", 50000).show();
} else {
// handle your error
e.printStackTrace();
Toast.makeText(getApplicationContext(),"error", 50000).show();
}
}
});
}
回答1:
I was able to fix this by adding the following dependency in build.grade:
dependencies {
compile 'com.google.android.gms:play-services-location:7.5.0'
}
I also had to remove all ACCESS_COARSE_LOCATION
and ACCESS_FINE_LOCATION
permissions from AndroidManifest.xml, which seems very strange.
The way you have defined the Criteria is correct, and also necessary.
This is a really nice feature in the Parse.com API but I wish they would provide some documentation. As is, I don't really trust it... I'm just using it as a quick and dirty solution until I have time to implement Google Play Services properly.
来源:https://stackoverflow.com/questions/28290728/getcurrentlocationinbackground-parse-com