I am developing my first Android app which should get the latitude and longitude of an android device and send it via a web service to a template document.
I followed the guide of getting the location from http://developer.android.com/training/location/retrieve-current.html.
This is the code from my .java class:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class GetLocation extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
EditText textLat;
EditText textLong;
EditText lat;
EditText lon;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.get_location);
textLat = (EditText) findViewById(R.id.latitude);
textLong = (EditText) findViewById(R.id.longitude);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
private boolean isGPSEnabled() {
LocationManager cm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
return cm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
@Override
public void onConnected(Bundle bundle) {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
lat.setText(String.valueOf(mLastLocation.getLatitude()));
lon.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public void onButtonClick(View v){
if(v.getId() == R.id.getGpsLocation){
if(!isGPSEnabled()){
new AlertDialog.Builder(this)
.setMessage("Please activate your GPS Location!")
.setCancelable(false)
.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
})
.setNegativeButton("Cancel", null)
.show();
} else {
textLat.setText(String.valueOf(lat));
textLong.setText(String.valueOf(lon));
}
}
}
}
I don't get any errors but when I am tapping the button which should get the coordinates, I get 'null' text in both views.
I also have included permissions for internet, access fine and coarse location.
Thanks in advance!
I have written detailed tutorial covering this topic here on demonuts.com.You can find more description here and also you can download whole demo source code for better understanding.
First of all, put this in gradle file
then implement necessary interfaces
declare instances
put this in
onCreate()
At last, override necessary methods
Don't forget to start GPS in your device before running app.
Here is a working example of user location:
https://github.com/keithweaver/Android-Samples/tree/master/Location
You need to define LocationListener .
And need to give below permission :
ACCESS_COARSE_LOCATION : It is used when we use network location provider for our Android app.
ACCESS_FINE_LOCATION : It is providing permission for both providers.
INTERNET : permission is must for the use of network provider.