I have successfully managed to get the current gps coordinates by calling requestLocationUpdates like this:
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
if( provider != null){
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
And my "onLocationChanged" is updating this position according to my criterias.
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "location is" + location);
double lat = (location.getLatitude());
double lng = (location.getLongitude());
String coordinates = (String.valueOf(lat) + "," + String.valueOf(lng));
longitudeField.setText(coordinates);
}
Now in my app I want to pass the latitude and longitude to firebase together with an image and some other information.
So I have another method that is taking the url from captured image and sending the image to firebase together with two strings that are the current date and time.
private void uploadFile() {
final String dateStamp = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
final String timeStamp = new SimpleDateFormat("HH:mm:ss").format(new Date());
if (mImageUri != null) {
StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgressBar.setProgress(0);
}
}, 500);
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
Upload upload = new Upload(
taskSnapshot.getDownloadUrl().toString(), timeStamp, dateStamp);
String uploadId = mDatabaseRef.push().getKey();
mDatabaseRef.child(dateStamp).child(uploadId).setValue(upload);
}
})
QUESTION:
How is the approach to getting the latitude and longitude in form of String so I can use it in my method for passing data to firebase?
I would recommend the following approach:
Seems pretty straightforward to me.