Am going to share a solution which include save and retrieve location inside a JPEG image file.The latitude and longitude is save and retrive inside the image metadata using ExifInterface
More about ExifInterface can be found here
http://developer.android.com/reference/android/media/ExifInterface.html
public void saveLocation() {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, latitude);
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, longitude);
exif.saveAttributes();
} catch (IOException e) {
e.printStackTrace();
}
}
public void retriveLocation() {
ExifInterface exif = null;
try {
exif = new ExifInterface(imagePath);
String[] latitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE).split(",");
String[] longitudeValue = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE).split(",");
String[] tmp = new String[2];
tmp = latitudeValue[0].split("/");
setLatitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
tmp = longitudeValue[0].split("/");
setLongitude(String.valueOf(Float.valueOf(tmp[0]) / Float.valueOf(tmp[1])));
} catch (IOException e) {
e.printStackTrace();
}
}
Using ExifInterface you can get following information from media.
Variable Declaration.
String mediaDateTime,attrLATITUDE,attrLATITUDE_REF,attrLONGITUDE,attrLONGITUDE_REF,zip, city,state, country;;
Double Latitude, Longitude;
List<Address> addresses;
Geocoder geocoder;
User ExifInterface using following way.
ExifInterface exifInterfaceMedia = new ExifInterface(<Your Image Path>);
// This will give you data and time
mediaDateTime = exifInterfaceMedia.getAttribute(ExifInterface.TAG_DATETIME);
attrLATITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
attrLATITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
attrLONGITUDE = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
attrLONGITUDE_REF = exifInterfaceMedia.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
valid = true;
if (attrLATITUDE_REF.equals("N")) {
Latitude = convertToDegree(attrLATITUDE);
} else {
Latitude = 0 - convertToDegree(attrLATITUDE);
}
if (attrLONGITUDE_REF.equals("E")) {
Longitude = convertToDegree(attrLONGITUDE);
} else {
Longitude = 0 - convertToDegree(attrLONGITUDE);
}
try {
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
zip = addresses.get(0).getPostalCode();
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
if (zip != null) {
title += zip + ",";
}
if (city != null) {
title += city + ",";
}
if (state != null) {
title += state + ",";
}
if (country != null) {
title += country;
}
} else {
title = "Unknown Location";
}
}
private Double convertToDegree(String stringDMS) {
Double result = null;
String[] DMS = stringDMS.split(",", 3);
String[] stringD = DMS[0].split("/", 2);
Double D0 = new Double(stringD[0]);
Double D1 = new Double(stringD[1]);
Double FloatD = D0 / D1;
String[] stringM = DMS[1].split("/", 2);
Double M0 = new Double(stringM[0]);
Double M1 = new Double(stringM[1]);
Double FloatM = M0 / M1;
String[] stringS = DMS[2].split("/", 2);
Double S0 = new Double(stringS[0]);
Double S1 = new Double(stringS[1]);
Double FloatS = S0 / S1;
result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));
return result;
}
Related
I have an app that find the shortest distance between my user to a polygon.
I want to convert the polygon to Geofence to check the distance between the user to the area to give mor accurate information to the user.
how can I do that?
this is the MapsActivity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, MinimumDistanceTask.GetMinimumDistanceListener {
private GoogleMap mMap;
private LocationManager manager;
private double lat, lng;
private KmlLayer layer;
private LatLng latLngTest;
private boolean contains = false;
private ArrayList<LatLng> outerBoundary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
#Override
protected void onResume() {
super.onResume();
String provider = LocationManager.GPS_PROVIDER;
//take the user location every second
try {
manager.requestLocationUpdates(provider, 1000, 0, this);
}catch (SecurityException e){
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
#Override
public void onLocationChanged(Location location) {
//clear map before create new location
mMap.clear();
try {
//load the kml file
layer = new KmlLayer(mMap, R.raw.polygon_layer, this);
layer.addLayerToMap();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
lat = location.getLatitude();
lng = location.getLongitude();
latLngTest = new LatLng(lat,lng);
// Add a marker in user location
LatLng userLocation = new LatLng(latLngTest.latitude, latLngTest.longitude);
mMap.addMarker(new MarkerOptions().position(userLocation).title("you are here"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 15));
//check if the user in the polygon
boolean inside = ifUserInside();
if(inside){
Toast.makeText(MapsActivity.this, "you are in the polygon", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MapsActivity.this, "you are outside the polygon", Toast.LENGTH_SHORT).show();
//create the string address for the url
String address = "";
for (int i = 0; i < outerBoundary.size(); i++) {
address += (outerBoundary.get(i).toString() + "|");
address = address.replace("lat/lng:", "");
address = address.replace(" ", "");
address = address.replace("(", "");
address = address.replace(")", "");
}
MinimumDistanceTask task = new MinimumDistanceTask(this);
task.execute("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins="+latLngTest.latitude+ "," + latLngTest.longitude
+ "&destinations=" + address + "&mode=walking");
}
}
#Override
public void getMinimumDistance(int closeLocation) {
//check if you get results properly
if(closeLocation != -1) {
GetDirection direction = new GetDirection();
direction.execute("https://maps.googleapis.com/maps/api/directions/json?origin=" + latLngTest.latitude + "," + latLngTest.longitude
+ "&destination=" + outerBoundary.get(closeLocation).latitude + "+" + outerBoundary.get(closeLocation).longitude);
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
public boolean ifUserInside(){
if (layer.getContainers() != null) {
for (KmlContainer container : layer.getContainers()) {
if (container.getPlacemarks() != null) {
for (KmlPlacemark placemark : container.getPlacemarks()) {
contains = false;
if (placemark.getGeometry() instanceof KmlPolygon) {
KmlPolygon polygon = (KmlPolygon) placemark.getGeometry();
// Get the outer boundary and check if the test location lies inside
outerBoundary = polygon.getOuterBoundaryCoordinates();
contains = PolyUtil.containsLocation(latLngTest, outerBoundary, true);
if (contains) {
// Get the inner boundaries and check if the test location lies inside
ArrayList<ArrayList<LatLng>> innerBoundaries = polygon.getInnerBoundaryCoordinates();
if (innerBoundaries != null) {
for (ArrayList<LatLng> innerBoundary : innerBoundaries) {
// If the test location lies in a hole, the polygon doesn't contain the location
if (PolyUtil.containsLocation(latLngTest, innerBoundary, true)) {
contains = false;
}
}
}
}
}
}
}
}
}
return contains;
}
public class GetDirection extends AsyncTask<String , Void, String> {
HttpsURLConnection connection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
#Override
protected String doInBackground(String... params) {
String address = params[0];
try {
URL url = new URL(address);
connection = (HttpsURLConnection) url.openConnection();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
return "Error from server";
}
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
#Override
protected void onPostExecute(String s) {
//get the polyline string
String polygonPoints = "";
try {
JSONObject object = new JSONObject(s);
JSONArray array = object.getJSONArray("routes");
for (int i = 0; i < array.length(); i++) {
JSONObject arrObj1 = array.getJSONObject(i);
JSONObject points = arrObj1.getJSONObject("overview_polyline");
polygonPoints = points.getString("points");
}
//convert the string to polyline;
ArrayList<LatLng> a = new ArrayList<>(decodePolyPoints(polygonPoints));
//add polyline to the map
mMap.addPolyline(new PolylineOptions().addAll(a).width(10).color(Color.BLUE));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//the method that convert the string to polyline
public static ArrayList<LatLng> decodePolyPoints(String encodedPath){
int len = encodedPath.length();
final ArrayList<LatLng> path = new ArrayList<LatLng>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < len) {
int result = 1;
int shift = 0;
int b;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lat += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
result = 1;
shift = 0;
do {
b = encodedPath.charAt(index++) - 63 - 1;
result += b << shift;
shift += 5;
} while (b >= 0x1f);
lng += (result & 1) != 0 ? ~(result >> 1) : (result >> 1);
path.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return path;
}
}
This is my AsyncTask to get the minimum distance point
public class MinimumDistanceTask extends AsyncTask<String, Void, Integer>{
private int closeLocation;
// private String points;
private GetMinimumDistanceListener listener;
public MinimumDistanceTask(GetMinimumDistanceListener listener){
// this.points = points;
this.listener = listener;
}
#Override
protected Integer doInBackground(String... params) {
HttpsURLConnection connection = null;
BufferedReader reader = null;
StringBuilder builder = new StringBuilder();
int minimumDis = -1;
String address = params[0];
try {
URL url = new URL(address);
connection = (HttpsURLConnection) url.openConnection();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK){
return -1;
}
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
builder.append(line);
}
///get the json data
JSONObject jsonObject1 = new JSONObject(builder.toString());
JSONArray points = jsonObject1.getJSONArray("rows");
JSONObject jsonObject2 = points.getJSONObject(0);
JSONArray elements = jsonObject2.getJSONArray("elements");
for (int i = 0; i < elements.length(); i++) {
JSONObject jsonObject3 = elements.getJSONObject(i);
JSONObject distance = jsonObject3.getJSONObject("distance");
if( distance.getInt("value") < minimumDis || minimumDis == -1) {
minimumDis = distance.getInt("value");
closeLocation = i;
}
}
} catch (MalformedURLException | JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return closeLocation;
}
#Override
protected void onPostExecute(Integer closeLocation) {
listener.getMinimumDistance(closeLocation);
}
public interface GetMinimumDistanceListener{
void getMinimumDistance(int closeLocation);
}
}
thanks a lot :)
You can use a function like the following to calculate the nearest point from polygon defined by a List<LatLng> and a given LatLng.
It uses the PolyUtil.distanceToLine from the Google Maps Android API Utility Library to compute the distance between the test LatLng and every segment of the list, and a method based on the distanceToLine method from https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java to compute the projection of a point on a segment.
private LatLng findNearestPoint(LatLng test, List<LatLng> target) {
double distance = -1;
LatLng minimumDistancePoint = test;
if (test == null || target == null) {
return minimumDistancePoint;
}
for (int i = 0; i < target.size(); i++) {
LatLng point = target.get(i);
int segmentPoint = i + 1;
if (segmentPoint >= target.size()) {
segmentPoint = 0;
}
double currentDistance = PolyUtil.distanceToLine(test, point, target.get(segmentPoint));
if (distance == -1 || currentDistance < distance) {
distance = currentDistance;
minimumDistancePoint = findNearestPoint(test, point, target.get(segmentPoint));
}
}
return minimumDistancePoint;
}
/**
* Based on `distanceToLine` method from
* https://github.com/googlemaps/android-maps-utils/blob/master/library/src/com/google/maps/android/PolyUtil.java
*/
private LatLng findNearestPoint(final LatLng p, final LatLng start, final LatLng end) {
if (start.equals(end)) {
return start;
}
final double s0lat = Math.toRadians(p.latitude);
final double s0lng = Math.toRadians(p.longitude);
final double s1lat = Math.toRadians(start.latitude);
final double s1lng = Math.toRadians(start.longitude);
final double s2lat = Math.toRadians(end.latitude);
final double s2lng = Math.toRadians(end.longitude);
double s2s1lat = s2lat - s1lat;
double s2s1lng = s2lng - s1lng;
final double u = ((s0lat - s1lat) * s2s1lat + (s0lng - s1lng) * s2s1lng)
/ (s2s1lat * s2s1lat + s2s1lng * s2s1lng);
if (u <= 0) {
return start;
}
if (u >= 1) {
return end;
}
return new LatLng(start.latitude + (u * (end.latitude - start.latitude)),
start.longitude + (u * (end.longitude - start.longitude)));
}
You can test it with the following code:
List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));
LatLng testPoint = new LatLng(3, 0);
LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)
Log.e("DISTANCE: ", "" + SphericalUtil.computeDistanceBetween(testPoint, nearestPoint)); // 222085.35856591124
I'm retrieving addresses from cooridnates using the Google API with the following method:
public static String[] getFromLocation(double lat, double lng, int retries) {
String address = String.format(Locale.getDefault(),
"https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language="
+ Locale.getDefault(), lat, lng);
String[] res = new String[3];
String addressLine = "";
String locality = "";
String country = "";
String json = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
URL url = new URL(address);
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
json = jsonResults.toString();
JSONObject jsonObject = new JSONObject(json);
if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
JSONArray results = jsonObject.getJSONArray("results");
if (results.length() > 0) {
JSONObject result = results.getJSONObject(0);
//Address addr = new Address(Locale.getDefault());
JSONArray components = result.getJSONArray("address_components");
String streetNumber = "";
String route = "";
for (int a = 0; a < components.length(); a++) {
JSONObject component = components.getJSONObject(a);
JSONArray types = component.getJSONArray("types");
for (int j = 0; j < types.length(); j++) {
String type = types.getString(j);
if (type.equals("locality")) {
locality = component.getString("long_name");
} else if (type.equals("street_number")) {
streetNumber = component.getString("long_name");
} else if (type.equals("route")) {
route = component.getString("long_name");
} else if (type.equals("country")) {
country = component.getString("long_name");
}
}
}
addressLine = route + " " + streetNumber;
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "Exception:", e);
LogsToServer.send(my_id, e);
if (json != null) LogsToServer.send(my_id, json);
System.out.println("retries: " + retries);
if (retries > 0){
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
return getFromLocation(lat, lng, retries-1);
}
}
res[0] = addressLine;
res[1] = locality;
res[2] = country;
return res;
}
The problem is that I very often get the exception:
03-12 23:54:01.387: E/GetAddressDetails(25248): java.io.FileNotFoundException: https://maps.googleapis.com/maps/api/geocode/json?latlng=48,2&sensor=false&language=en_GB
03-12 23:54:01.387: E/GetAddressDetails(25248): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:197)
03-12 23:54:01.387: E/GetAddressDetails(25248): at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
03-12 23:54:01.387: E/GetAddressDetails(25248): at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
If I launch the method with 4 retries, they may all fail, or sometimes after 2 or 3 I get the address. Do you know why it fails so often? When I access the same site in my browser I always get the page without errors!
EDIT: I checked the error message returned by Google and it goes like this:
We're sorry... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.
Is it a joke? Automated queries? Isn't it the whole purpose of APIs to be called by automatic processes?
Also, this happens from many phones and started yesterday. How does google know that all the requests come from the same app?
If you want to get address from cooridnates, you can use Geocoder api.
I used following code in my app to get the city name:
private double mLongitude; // current longitude
private double mLatitude; // current latitude
private String mCityName; // output cityName
private void getCityName() {
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(mLatitude, mLongitude, 1);
if (addresses.size() != 0) {
mCityName = addresses.get(0).getAddressLine(1);
mCityName = mCityName.replaceAll("[\\d.]", "");
Log.d(TAG + "!!!!!!!!!!!", mCityName);
}
} catch (IOException e) {
e.printStackTrace();
}
}
If you want get whole address, as I changed a little bit, just do this way:
private double mLongitude; // current longitude
private double mLatitude; // current latitude
private String mAddress; // output address
private void getAddress() {
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(mLatitude, mLongitude, 1);
if (addresses.size() != 0) {
mAddress = addresses.get(0).getAddressLine(0) + " " +
addresses.get(0).getAddressLine(1) + " " +
addresses.get(0).getAddressLine(2);
//mAddress = mAddress.replaceAll("[\\d.]", "");
Log.d(TAG + "!!!!!!!!!!!", mAddress);
}
} catch (IOException e) {
e.printStackTrace();
}
}
I'm developing an app. In that I get time, date, longitude, and latitude.
All info get successfully.
Now I want to format date and time which I get from ExifInterface.TAG_DATETIME.
How can I do that?
MainActivity.java
public class DisplayImage extends Activity {
ImageView displayImage;
VideoView displayVideo;
TextView imgInfo;
DatabaseHelper dbHelper;
String mediaPath = null;
private boolean valid = false;
ExifInterface exifInterface;
LocationManager locationManager;
Location location;
Geocoder geocoder;
List<Address> addresses;
Double Latitude, Longitude;
String zip, city, state, country;
String imgDate_Time, attrLATITUDE, attrLATITUDE_REF, attrLONGITUDE, attrLONGITUDE_REF;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_image);
displayImage = (ImageView) findViewById(R.id.displayImage);
displayVideo = (VideoView) findViewById(R.id.displayVideo);
imgInfo = (TextView) findViewById(R.id.txtImgInfo);
dbHelper = new DatabaseHelper(getApplicationContext());
mediaPath = dbHelper.displayImages();
File mediaFile = new File(mediaPath);
String title = "";
geocoder = new Geocoder(DisplayImage.this, Locale.getDefault());
try {
exifInterface = new ExifInterface(mediaPath);
imgDate_Time = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
attrLATITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
attrLATITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
attrLONGITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
attrLONGITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null)
&& (attrLONGITUDE_REF != null)) {
valid = true;
if (attrLATITUDE_REF.equals("N")) {
Latitude = convertToDegree(attrLATITUDE);
} else {
Latitude = 0 - convertToDegree(attrLATITUDE);
}
if (attrLONGITUDE_REF.equals("E")) {
Longitude = convertToDegree(attrLONGITUDE);
} else {
Longitude = 0 - convertToDegree(attrLONGITUDE);
}
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
if (addresses != null && addresses.size() > 0) {
zip = addresses.get(0).getPostalCode();
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
if (zip != null) {
title += zip + ",";
}
if (city != null) {
title += city + ",";
}
if (state != null) {
title += state + ",";
}
if (country != null) {
title += country;
}
imgInfo.setText(imgDate_Time + " " + Latitude + " " + Longitude + " "
+ title);
} else {
title = "Unknown Location";
imgInfo.setText("Address Not Found");
}
} else {
if (imgDate_Time != null) {
imgInfo.setText(imgDate_Time);
} else {
imgInfo.setText("Media Information Not Available");
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (mediaFile.exists()) {
if (isImage(mediaPath)) {
displayVideo.setVisibility(View.GONE);
displayImage.setVisibility(View.VISIBLE);
Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
displayImage.setImageBitmap(scale);
} else {
displayImage.setVisibility(View.GONE);
displayVideo.setVisibility(View.VISIBLE);
displayVideo.setVideoURI(Uri.parse(mediaFile.toString()));
displayVideo.start();
}
}
}
private Double convertToDegree(String stringDMS) {
Double result = null;
String[] DMS = stringDMS.split(",", 3);
String[] stringD = DMS[0].split("/", 2);
Double D0 = new Double(stringD[0]);
Double D1 = new Double(stringD[1]);
Double FloatD = D0 / D1;
String[] stringM = DMS[1].split("/", 2);
Double M0 = new Double(stringM[0]);
Double M1 = new Double(stringM[1]);
Double FloatM = M0 / M1;
String[] stringS = DMS[2].split("/", 2);
Double S0 = new Double(stringS[0]);
Double S1 = new Double(stringS[1]);
Double FloatS = S0 / S1;
result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));
return result;
}
public boolean isValid() {
return valid;
}
#Override
public String toString() {
// TODO Auto-generated method stub
return (String.valueOf(Latitude) + ", " + String.valueOf(Longitude));
}
public int getLatitudeE6() {
return (int) (Latitude * 1000000);
}
public int getLongitudeE6() {
return (int) (Longitude * 1000000);
}
public static boolean isImage(String str) {
boolean temp = false;
String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
for (int i = 0; i < arr.length; i++) {
temp = str.endsWith(arr[i]);
if (temp) {
break;
}
}
return temp;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.splash_screen, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
A rewritten version of the code posted by Chirag to parse the TAG into a Joda time object:
/**
* Converts an ExifInterface time and date tag into a Joda time format
*
* #param EXIF_TAG_DATETIME
* #return null in case of failure, the date object otherwise
*/
public static LocalDateTime convert( String EXIF_TAG_DATETIME){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.US);
try {
return new LocalDateTime( simpleDateFormat.parse( EXIF_TAG_DATETIME ) );
} catch (ParseException e) {
Log.e(TAG, e.getMessage());
return null;
}
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
SimpleDateFormat convertDate = new SimpleDateFormat("MMM dd, yyyy");
SimpleDateFormat convertTime = new SimpleDateFormat("hh:mm:ss aa");
Date d = null, d2 = null;
try {
d = simpleDateFormat.parse(imgDate_Time);
d2 = simpleDateFormat.parse(imgDate_Time);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String dateFormat = convertDate.format(d);
String timeFormat = convertTime.format(d2);
Now Print dateFormat and TimeFormat. and dateFormat and timeFormat is String Variable.
I have an app published on Google Play, which at some point fetches the users geographical location and saves it with a picture. I have however now gotten reports from a few Brazilian users complaining about the pictures are getting GeoTagged in Saudi Arabia. Have only gotten this complaint from users in Brazil and I am by no means what so ever able to reproduce the issue myself.
What could be causing this?
// I save and read the location in an ExifInterface with this "book"..
// Write Geotag and time to Image's Exif
public Boolean writeExifData(Location location, Date date, String filepath){
// Determine if we got a location
boolean isLocationOn = false;
if(location != null){ isLocationOn = true; }
try {
// Set up exif
ExifInterface exif = new ExifInterface(filepath);
if(isLocationOn){
// Get location values
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float accuracy = location.getAccuracy();
// Convert Latitude and Longitude
int num1Lat = (int)Math.floor(latitude);
int num2Lat = (int)Math.floor((latitude - num1Lat) * 60);
double num3Lat = (latitude - ((double)num1Lat+((double)num2Lat/60))) * 3600000;
int num1Lon = (int)Math.floor(longitude);
int num2Lon = (int)Math.floor((longitude - num1Lon) * 60);
double num3Lon = (longitude - ((double)num1Lon+((double)num2Lon/60))) * 3600000;
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, num1Lat+"/1,"+num2Lat+"/1,"+num3Lat+"/1000");
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, num1Lon+"/1,"+num2Lon+"/1,"+num3Lon+"/1000");
if (latitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "N");
}else {
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, "S");
}
if (longitude > 0) {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "E");
}else {
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, "W");
}
// Add accuracy to exif
String accuracy_value = String.valueOf(Math.round(accuracy));
exif.setAttribute("UserComment", "accuracy=" + accuracy_value);
}
// Add capture time to exif
exif.setAttribute(ExifInterface.TAG_DATETIME, date.toString());
// Save exif
exif.saveAttributes();
Log.v(Constants.TAG_ALERT_PROCESS, "Wrote data to Exif successfully");
return true;
} catch (IOException e) {
Log.v(Constants.TAG_ALERT_PROCESS, "Failed writing data to Exif with error: " + e.getMessage());
return false;
}
}
public Object_ImageExifData readExifData(String filePath){
Helper_ExifGeoConverter geoConverter;
Location location = null;
ExifInterface exif;
// Format filePath String (remove "file://" )
if(filePath.startsWith("file://")){
filePath = filePath.replace("file://", "");
}
try {
exif = new ExifInterface(filePath);
if(exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE) != null){
geoConverter = new Helper_ExifGeoConverter(exif);
location = new Location("");
float float_lat = geoConverter.getLatitude();
location.setLatitude(float_lat);
float float_lon = geoConverter.getLongitude();
location.setLongitude(float_lon);
/** Get accuracy. Accuracy is stored
* under the tag UserComment as "accuracy=xx"*/
if(exif.getAttribute("UserComment") != null){
String value = exif.getAttribute("UserComment");
value = value.replace("accuracy=", "");
float accuracy = Integer.parseInt(value);
location.setAccuracy(accuracy);
}
// Get capture time
String time = "";
if(exif.getAttribute(ExifInterface.TAG_DATETIME) != null){
time = exif.getAttribute(ExifInterface.TAG_DATETIME);
}
return new Object_ImageExifData(location, time);
}else{
// Get capture time
String time = "xx.xx.xxxx";
if(exif.getAttribute(ExifInterface.TAG_DATETIME) != null){
time = exif.getAttribute(ExifInterface.TAG_DATETIME);
}
return new Object_ImageExifData(location, time);
}
} catch (IOException e) {
e.printStackTrace();
Log.v("myTag", "Failed to read exif data");
return null;
}
}
public class Helper_ExifGeoConverter{
private boolean valid = false;
Float Latitude, Longitude;
Helper_ExifGeoConverter(ExifInterface exif) {
String attrLATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String attrLATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String attrLONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
if ((attrLATITUDE != null) && (attrLATITUDE_REF != null)
&& (attrLONGITUDE != null) && (attrLONGITUDE_REF != null)) {
valid = true;
if (attrLATITUDE_REF.equals("N")) {
Latitude = convertToDegree(attrLATITUDE);
} else {
Latitude = 0 - convertToDegree(attrLATITUDE);
}
if (attrLONGITUDE_REF.equals("E")) {
Longitude = convertToDegree(attrLONGITUDE);
} else {
Longitude = 0 - convertToDegree(attrLONGITUDE);
}
}
}
private Float convertToDegree(String stringDMS){
Float result;
String[] DMS = stringDMS.split(",", 3);
String[] stringD = DMS[0].split("/", 2);
Double D0 = Double.valueOf(stringD[0]);
Double D1 = Double.valueOf(stringD[1]);
Double FloatD = D0/D1;
String[] stringM = DMS[1].split("/", 2);
Double M0 = Double.valueOf(stringM[0]);
Double M1 = Double.valueOf(stringM[1]);
Double FloatM = M0/M1;
String[] stringS = DMS[2].split("/", 2);
Double S0 = Double.valueOf(stringS[0]);
Double S1 = Double.valueOf(stringS[1]);
Double FloatS = S0/S1;
result = new Float(FloatD + (FloatM/60) + (FloatS/3600));
return result;
}
SOLUTION
With the help of #greenapps I came to realize that I was, when writing to Exif, converting location from decimal format to degrees, minutes, seconds format falsely. Used this code instead.
If you put geo lat/lon tags in the exif info of a jpg then all lat/lons are positive. You also have to set a E/O and N/S tag. Are you doing that?
From my application I can open gallery. Is there any way to get latitude and longitude of any selected image in gallery to my application?
You can actually use a "buildin" function:
ExifInterface exif = new ExifInterface(path);
float[] latLong = new float[2];
boolean hasLatLong = exif.getLatLong(latLong)
if (hasLatLong) {
System.out.println("Latitude: " + latLong[0]);
System.out.println("Longitude: " + latLong[1]);
}
Maybe is something new, but I think is much more convenient than the accepted answer.
You Should Go with ExifInterface class to read various EXIF metadata from Images:
Example :
ExifInterface exif = new ExifInterface(filepath);
exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
Edited :
Now Here you will get lat-long as Below format.
lat = 30/1,12/1,34/1,
long=81/1,22/1,41/1
To Convert this into Real Values this Blog Helped Me.
we need to do conversion from degree, minute, second form to GeoPoint form.
By Below Way you can Do it.
String LATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String LATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
String LONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
String LONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
// your Final lat Long Values
Float Latitude, Longitude;
if((LATITUDE !=null)
&& (LATITUDE_REF !=null)
&& (LONGITUDE != null)
&& (LONGITUDE_REF !=null))
{
if(LATITUDE_REF.equals("N")){
Latitude = convertToDegree(LATITUDE);
}
else{
Latitude = 0 - convertToDegree(LATITUDE);
}
if(LONGITUDE_REF.equals("E")){
Longitude = convertToDegree(LONGITUDE);
}
else{
Longitude = 0 - convertToDegree(LONGITUDE);
}
}
private Float convertToDegree(String stringDMS){
Float result = null;
String[] DMS = stringDMS.split(",", 3);
String[] stringD = DMS[0].split("/", 2);
Double D0 = new Double(stringD[0]);
Double D1 = new Double(stringD[1]);
Double FloatD = D0/D1;
String[] stringM = DMS[1].split("/", 2);
Double M0 = new Double(stringM[0]);
Double M1 = new Double(stringM[1]);
Double FloatM = M0/M1;
String[] stringS = DMS[2].split("/", 2);
Double S0 = new Double(stringS[0]);
Double S1 = new Double(stringS[1]);
Double FloatS = S0/S1;
result = new Float(FloatD + (FloatM/60) + (FloatS/3600));
return result;
};
#Override
public String toString() {
// TODO Auto-generated method stub
return (String.valueOf(Latitude)
+ ", "
+ String.valueOf(Longitude));
}
public int getLatitudeE6(){
return (int)(Latitude*1000000);
}
public int getLongitudeE6(){
return (int)(Longitude*1000000);
}
the exif.getLatLong(float[]) is now deprecated, you can use a better method which returns a double[] :
ExifInterface exifInterface = new ExifInterface(file);
double[] latlng = exifInterface.getLatLong();
if (latlng != null) {
Double currentLatitude = latlng[0];
Double currentLongitude = latlng[1];
Log.d("Debug", "Exif : latitude: " + currentLatitude + ", longitude: " + currentLongitude)
}
Happy coding.