Thanos Infosec
Thanos Infosec

Reputation: 57

GPS location tagging of photos in Android

enter image description hereI am trying to create an app to take photos using mobile's camera, save them, attach GPS coordinates on the captured image. I am able to take a picture, save it to the mobile's album, but not able to display the GPS location (Lat,Lon) in a tittle bar on the image. I use exif but not sure if I am correct. Could you please advice me how to display coordinates on my image?

public class MainActivity extends AppCompatActivity {

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File folder = new File(Environment.getExternalStorageDirectory() + "/PhotoGPSApp");
    if (folder.exists() == false) {
        folder.mkdirs();
    }

    Button btnCamera = (Button) findViewById(R.id.btnCamera);
    imageView = (ImageView) findViewById(R.id.imageView);

    btnCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/PhotoGPSApp/Attachment" + ".jpg")));
            startActivityForResult(intent, 0);

        }
    });
}

LocationManager locationManager;

boolean gotLocation = false;

double longitude = 0.0;
double latitude = 0.0;

public boolean validLatLng (double lat, double lng) {
    if(lat != 0.0 && lng != 0.0){
        this.gotLocation = true;
        return true;
    } else return false;
}

public boolean haveLocation() {
    return this.gotLocation;
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
    imageView.setImageBitmap(bitmap);

    if (resultCode == RESULT_OK) {

        // Get LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        locationManager.requestLocationUpdates(provider, 1000, 0.0f, mLocationListener); // (String) provider, time in milliseconds when to check for an update, distance to change in coordinates to request an update, LocationListener.


    }

}

LocationListener mLocationListener = new LocationListener() {
    public void onLocationChanged (Location location){
        if (!haveLocation() && validLatLng(location.getLatitude(), location.getLongitude())) {
            //System.out.println("got new location");
            //Log.i("mLocationListener", "Got location");   // for logCat should ->  import android.util.Log;

            // Stops the new update requests.
            locationManager.removeUpdates(mLocationListener);
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            File f = new File(Environment.getExternalStorageDirectory(), "/PhotoGPSApp/Attachment" + ".jpg");
            geoTag(f.getAbsolutePath(), latitude, longitude);

        }
    }

    public void onStatusChanged(java.lang.String s, int i, android.os.Bundle bundle) {
    }

    public void onProviderEnabled(java.lang.String s){
    }

    public void onProviderDisabled(java.lang.String s){
    }

};



public void geoTag(String filename, double latitude, double longitude){
    ExifInterface exif;

    try {
        exif = new ExifInterface(filename);
        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");
        }

        exif.saveAttributes();

    } catch (IOException e) {
        Log.e("PictureActivity", e.getLocalizedMessage());
    }

}
   }

My activity_main.xml is:

<ImageView
    android:id="@+id/imageView"
    android:layout_weight="9"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" />

<Button
    android:id="@+id/btnCamera"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@android:color/holo_red_dark"
    android:textColor="@android:color/white"
    android:text="Open Camera"
    />

Upvotes: 1

Views: 6026

Answers (1)

Thanos Infosec
Thanos Infosec

Reputation: 57

Problem solved. I used textView.setText with GPS coordinates and I updated my activity_main.xml file to show in text form.

Upvotes: 0

Related Questions