Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the latitude and longitude of an image in sdcard to my application?

Tags:

android

gps

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?

like image 476
SREEJITH Avatar asked Mar 14 '13 07:03

SREEJITH


People also ask

How do I find the latitude and longitude of a picture?

Find the GPS Coordinates In Windows, all you have to do is right-click a picture file, select “Properties,” and then click the “Details” tab in the properties window. Look for the Latitude and Longitude coordinates under GPS.

How can I get latitude and longitude from address in Android?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);

What is Exifinterface?

Exchangeable Image File Format (Exif) is an acronym for Exchangeable Image File Format. This is a standard that specifies detailed information about a photograph or other piece of media recorded by a camera. It may save critical information like camera exposure, the date/time the image was taken, and even GPS position.


2 Answers

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);
}
like image 147
Bhavesh Patadiya Avatar answered Oct 27 '22 01:10

Bhavesh Patadiya


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.

like image 41
pauminku Avatar answered Oct 27 '22 01:10

pauminku