mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Added LocationListener
This commit is contained in:
parent
f0b9d35bfd
commit
0c7c2d745e
1 changed files with 50 additions and 7 deletions
|
|
@ -1,10 +1,16 @@
|
||||||
package fr.free.nrw.commons.upload;
|
package fr.free.nrw.commons.upload;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.location.Criteria;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
|
import android.location.LocationListener;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.media.ExifInterface;
|
import android.media.ExifInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.provider.Settings;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
|
@ -13,9 +19,11 @@ public class GPSExtractor {
|
||||||
|
|
||||||
private String filePath;
|
private String filePath;
|
||||||
private double decLatitude, decLongitude;
|
private double decLatitude, decLongitude;
|
||||||
|
private double currentLatitude, currentLongitude;
|
||||||
private Context context;
|
private Context context;
|
||||||
private static final String TAG = GPSExtractor.class.getName();
|
private static final String TAG = GPSExtractor.class.getName();
|
||||||
public boolean imageCoordsExists;
|
public boolean imageCoordsExists;
|
||||||
|
private MyLocationListener myLocationListener;
|
||||||
|
|
||||||
public GPSExtractor(String filePath, Context context){
|
public GPSExtractor(String filePath, Context context){
|
||||||
this.filePath = filePath;
|
this.filePath = filePath;
|
||||||
|
|
@ -43,16 +51,26 @@ public class GPSExtractor {
|
||||||
imageCoordsExists = false;
|
imageCoordsExists = false;
|
||||||
Log.d(TAG, "Picture has no GPS info");
|
Log.d(TAG, "Picture has no GPS info");
|
||||||
|
|
||||||
LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
|
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
|
||||||
Location getLastLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
|
Criteria criteria = new Criteria();
|
||||||
double currentLongitude = getLastLocation.getLongitude();
|
String provider = locationManager.getBestProvider(criteria, true);
|
||||||
double currentLatitude = getLastLocation.getLatitude();
|
myLocationListener = new MyLocationListener();
|
||||||
Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
|
|
||||||
|
locationManager.requestLocationUpdates(provider, 400, 1, myLocationListener);
|
||||||
|
Location location = locationManager.getLastKnownLocation(provider);
|
||||||
|
|
||||||
|
if (location != null) {
|
||||||
|
myLocationListener.onLocationChanged(location);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//calling method is equipped to deal with null return value
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Log.d(TAG, "Current location values: Lat = " + currentLatitude + " Long = " + currentLongitude);
|
||||||
String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
|
String currentCoords = String.valueOf(currentLatitude) + "|" + String.valueOf(currentLongitude);
|
||||||
return currentCoords;
|
return currentCoords;
|
||||||
}
|
|
||||||
else {
|
} else {
|
||||||
imageCoordsExists = true;
|
imageCoordsExists = true;
|
||||||
Log.d(TAG, "Picture has GPS info");
|
Log.d(TAG, "Picture has GPS info");
|
||||||
|
|
||||||
|
|
@ -68,6 +86,31 @@ public class GPSExtractor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class MyLocationListener implements LocationListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLocationChanged(Location location) {
|
||||||
|
currentLatitude = location.getLatitude();
|
||||||
|
currentLongitude = location.getLongitude();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
|
Log.d(TAG, provider + "'s status changed to " + status);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProviderEnabled(String provider) {
|
||||||
|
Log.d(TAG, "Provider " + provider + " enabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onProviderDisabled(String provider) {
|
||||||
|
Log.d(TAG, "Provider " + provider + " disabled");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public double getDecLatitude() {
|
public double getDecLatitude() {
|
||||||
return decLatitude;
|
return decLatitude;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue