2

I have a method that, given a Uri, should retrieve the location data from that photo. However, all I am getting is zeros from the cursor.GetDouble(latitudeColumnIndex); method

What am I missing?

private void GetImageLocation(Uri uri)
{
    string[] projection =
    {
        MediaStore.Images.Media.InterfaceConsts.Latitude,
        MediaStore.Images.Media.InterfaceConsts.Longitude, 

    };

    using (ICursor cursor = ContentResolver.Query(uri, projection, null, null, null))
    {
        if (cursor.MoveToFirst())
        {
            int latitudeColumnIndex = cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Latitude);
            int longitudeColumnIndex = cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Longitude);

            if (latitudeColumnIndex == -1 || longitudeColumnIndex == -1)
            {
                _newPhoto.Latitude = 0;
                _newPhoto.Longitude = 0;
            }

            _newPhoto.Latitude = cursor.GetDouble(latitudeColumnIndex);
            _newPhoto.Longitude = cursor.GetDouble(longitudeColumnIndex);
        }
        else
        {
            _newPhoto.Latitude = 0;
            _newPhoto.Longitude = 0;
        }

        cursor.Close();
    }
}
William Barbosa
  • 4,936
  • 2
  • 19
  • 37
ChrisSmathers
  • 117
  • 2
  • 13
  • try [this answer](http://stackoverflow.com/questions/11187717/get-latitude-and-longitude-from-image-camera-android) or [this answer](http://stackoverflow.com/questions/15403797/how-to-get-the-latititude-and-longitude-of-an-image-in-sdcard-to-my-application) seems to be a good solution... – Old Fox Aug 17 '15 at 15:52
  • Thanks for the reply, I tried.... 'ExifInterface exif = new ExifInterface(uri.Path);' 'var a = exif.GetAttribute(ExifInterface.TagGpsLatitude);' 'var b = exif.GetAttribute(ExifInterface.TagGpsLatitudeRef);' 'var c = exif.GetAttribute(ExifInterface.TagGpsLongitude);' 'var d = exif.GetAttribute(ExifInterface.TagGpsLongitudeRef);' Everything came back as null. Wonder if it is a Xamarin bug? – ChrisSmathers Aug 17 '15 at 16:07
  • A year ago was the last time I created an android application... However I remember you have to request a permissions... Did you specify the right permissions? – Old Fox Aug 17 '15 at 17:08

1 Answers1

2

You should try using ExifInterface.GetLatLong.

It receives a float array (where the latitude and longitude will be stored) and returns a bool indicating whether the operation succeeded or not. Its usage is something like this:

var exif = new ExifInterface(uri.Path);
var latLong = new float[2];
float lat, long;

//Check if Latitude and Longitude can be retrieved
if(exif.GetLatLong(latLong))
{
    lat = latLong[0];
    long = latLong[1];
}
else
{
    //Fallback
    lat = 0;
    long = 0;
}
William Barbosa
  • 4,936
  • 2
  • 19
  • 37