2
votes

I was working on Google maps, I need Google map driving direction between two locations(my current location and destination location) in my own application I don't want to open any google maps application. so please suggest me how to do this. up to now i have completed integrating google maps, zoom to my current location, placing a marker in destination lat-long.

my java file:

public class GoogleMapActivity extends FragmentActivity implements
    OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

Location mLastLocation;
GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;

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

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Log.d("Tag", "in onc control in try");
    buildGoogleApiClient();
}

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        Log.d("TAG","lat"+mLastLocation.getLatitude());
        Log.d("TAG","lng"+mLastLocation.getLongitude());
        ToastHelper.blueToast(getApplicationContext(), "location is " + mLastLocation.getLatitude() + " " + mLastLocation.getLongitude());
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 12.0f));
        LatLng destination = new LatLng(14.880499, 79.988847);
        mMap.addMarker(new MarkerOptions().position(destination).title("Destination"));
    }
    else {
        Log.d("TAG","mLastLocation is null");
    }
}

@Override
public void onConnectionSuspended(int i) {
}

/**
 * Called when the map is ready.
 */
@Override
public void onMapReady(GoogleMap map) {
    mMap = map;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);
}
/**
 * Called when the user clicks a marker.
 */
@Override
public boolean onMarkerClick(final Marker marker) {
    // Retrieve the data from the marker.
    // Return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false;
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

4
Why are you choosing to do that? It is doable but get prepared to do a looot of work yourself.The_Martian
thanks for the suggestion @Martian, is their any direct google api to get the directions between two locations?Ram Koti

4 Answers

0
votes

There is a webservice public API for directions by google. It is not bundled with android API as far as I know.

Here you go with the link which will get you started

0
votes

Check out this Awesome and easy to use Library: https://github.com/jd-alexander/Google-Directions-Android

  • This library allows you to calculate the route between two locations and displays it on a map.
  • It provides you a list of routing point which you can plot in Google Map to show routes and directions.
0
votes

I don't know the end goal of your project. But if your intention is to get directions from point A to point B. Google's Roads Api can provide you with the data you need to draw the directions on a map or whatever you want.

It provides you the raw points even the speed in that particular road segment. It takes care of the redundant data that might lead you to draw directions through buildings for instance.

-1
votes

Finally got the result: use this API link: http://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination)

where it will return JSON as a response which contains latitudes and longitudes, and store them in an array and draw polylines using those lat-long points in google maps