if you are using google maps api then you must be using the googleapiclient in your app. if not then please do so.
using google api client-
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.enableAutoManage(this, 0, this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.build();
@Override
protected void onStart() {
googleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
if (googleApiClient != null && googleApiClient.isConnected()) {
googleApiClient.disconnect();
}
super.onStop();
}
then after connecting to google ai client you will have to implement methods and inside onConnected() method (you will have to override this method) you can get the current location.
getting current location inside onConnected-
Location loc = LocationServices
.FusedLocationApi
.getLastLocation(googleApiClient);
the loc object is the current location and you can use in any point of code to get the location at that instance , but only after the googleapiclient is connected.
you can apply this into your code as-
String url="http://maps.google.com/maps/api/directions/json?origin="+loc.getLatitude()+","+loc.getLongitude()+"&destination=41.311725,-72.740211";