Faster way, using the Google Api, not Android Api:
First, make your class extend GoogleApiClient.ConnectionCallbacks and LocationListener interfaces, and declare a GoogleApiClient variable.
private GoogleApiClient gApiClient;
Second, call this method from onCreate or similar:
protected synchronized void buildGoogleApiClient() {
gApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
})
.addApi(LocationServices.API)
.build();
gApiClient.connect();
createLocationRequest();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
Finally override this method:
@Override
public void onLocationChanged(Location location) {
tempLatitude=location.getLatitude();
tempLongitude=location.getLongitude();
}
---EDIT---
I just realized I was missing a method, this one, called automatically after you connect the fApiClient:
@Override
public void onConnected(Bundle bundle) {
Location lkn= LocationServices.FusedLocationApi.getLastLocation(
gApiClient);
if(lkn!=null){
tempLatitud=lkn.getLatitude();
tempLongitud=lkn.getLongitude();
}
LocationServices.FusedLocationApi.requestLocationUpdates(
gApiClient, mLocationRequest, this);
}
And, if you are using a map, you will need to extend another two interfaces: OnMapReadyCallback, and GoogleMap.OnMapClickListener, this last in case you need to react to the user tapping the map. The first one only need to implement method onMapReady(GoogleMap map). Inside that method, you can do
this.map=map;
(this.map refers to a GoogleMap variable declared by you, in your class).
And after that, your map will be ready to be used.
You will have your updates of the location of the device in onLocationChanged method for you to use them.
Answering your second question, you can get your latitude and longitude by triangulation of cell towers. Look here