0

I have an application, in which I want to send my actual coordinates through email.

The code for opening a email client is the following:

final Button button = (Button) findViewById(R.id.addbutton);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

          //Funktion aufrufen, aber ausserhalb definieren

        Intent i = new Intent(Intent.ACTION_SENDTO);
        //i.setType("message/rfc822");
        i.setData(Uri.parse("mailto:"));
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"mymail@example.com"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email";
        try {
            startActivity(i);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MapsActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

    }
});

The button like this works fine.

Now I thought that I can define two variables with the actual latitude and the other with the current longitude.

For this purpose, I would have to get the actual coordinates.

So my question is: how can I put my current GPS coordinates in an email.

And please: keep the code as simple as possible :)

Edit:

I have this one: (which I don't really understand)

public void getLocation() {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

and this one:

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

@Override
public void onLocationChanged(Location location) {
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();

    location_lat = latitude;
    location_long = longitude;
}

Error I'm getting. All three Errors are in line 77

MrHarrison
  • 15
  • 1
  • 9

1 Answers1

0

Add this permission to the manifest(API version<23)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Declare two variables to store the coordinates and one variable of Location

Location location;
double latitude;
double longitude;

Replace your getLocation() method with this

public void getLocation(){
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    if (locationManager != null) {
        try {
            location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } catch (SecurityException e) {

        }
        if (location != null) {
            latitude = location.getLatitude();
            longitude = location.getLongitude();
        }
    }
}

After getting coordinates pass this in the email body.

...
i.putExtra(Intent.EXTRA_TEXT   , "Longitude "+longitude+", Latitude "+latitude);
...
Ranjan
  • 390
  • 2
  • 10