0

I'm using google map direction API to get direction json, but my problem is I cannot pass current location to the origin parameter. Is there any way to do this?

http://maps.google.com/maps/api/directions/json?origin=41.393529,-72.811514&destination=41.311725,-72.740211
asok Buzz
  • 1,876
  • 3
  • 24
  • 50

2 Answers2

0

Take a look at the documentation. It looks like you are using the wrong url. The documentation for example lists https://maps.googleapis.com/maps/api/directions/json?origin=Brooklyn&destination=Queens as the right API. So maybe change maps.google.com to maps.googleapis.com?

EDIT:

Ah now I get the problem. It looks like this is not provided by the Google Maps API. Guess the best way to go is by using the Location Manager on your Android phone to get the latitude and longitude and format them in the url.

0

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";
Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52