hPys
hPys

Reputation: 133

Showing Driver Route between to points in Google Maps

I'm doing a simple app using Google Maps v2. All i want is to draw a driver route between two points. I've put polyline but it just draw straight line. The line should track the road. I just don't know how to. Thanks in Advance

This code is just getting the current position, i just set the other latitude and longitude for testing.

here's my code

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;

public class MainActivity extends Activity {

GoogleMap googleMap;
GPSTracker gps;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //create class GPS enabled
    gps = new GPSTracker(MainActivity.this);
    //check if GPS enabled
    if(gps.canGetLocation()){
        try{
            initializeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        double latitude2 = 13.768000;
        double longitude2 = 122.9768100;

        GoogleMap mMap;

        MarkerOptions Marker2 = new MarkerOptions().position(new LatLng(latitude2,    longitude2)).title("2222");
        googleMap.addMarker(Marker2);
        Marker2.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        //create Marker
        MarkerOptions Marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("iHatid Maps");
        //add marker
        googleMap.addMarker(Marker);
        // color of marker
        Marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        //camera motion location
        CameraPosition cameraPosition = CameraPosition.builder().target(new LatLng(latitude, longitude)).zoom(20).build();
        googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

     } else {
        //can't get location
        //GPS or Network is not enabled
        //ask user to enable GPS/Network in settings
        gps.showSettingsAlert();
    }
}

private void initializeMap() {
    if(googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

        if(googleMap == null) {
            Toast.makeText(getApplicationContext(), 
                    "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
        }
    }
}

 @Override
    protected void onResume() {
        super.onResume();
        initializeMap();
    }


}

Upvotes: 0

Views: 2497

Answers (2)

Ranjit
Ranjit

Reputation: 5150

Use the path information provided by Google in https://maps.googleapis.com/maps/api/directions/json....

Just look at this link here.

You can find all information between two geopoints, like distance(on road), duration with both in walk and driving mode.

You just have to give two inputs(origin and destination lat lon) and parse the json provided there.

For coding help you can follow this tutorial.

Upvotes: 2

Amine Chikhaoui
Amine Chikhaoui

Reputation: 345

You can use the google direction API by just passing the two points as arguments and the google web service will return the route in JSON, then you can parse the data, extract the different points and finally show the route.

For further informations, check the google direction API documentation.

Upvotes: 0

Related Questions