Skip to content Skip to sidebar Skip to footer

Create Itinary Google Maps Android

After using gps for geolocalization, i want to create an line to show the itinary between two points. I know i need to use the polyline but after the JSON parsing, I don't know how

Solution 1:

I think this could help, the "getDocument()" do all the work

@OverridepublicvoidonMapReady(GoogleMap googleMap) {
        GMapV2Directionmd=newGMapV2Direction();
        Documentdoc= md.getDocument(formatAddress(ORIGIN_TEXT, formatAddress(DESTINATION_TEXT, MODE, getApplicationContext());
        if (doc != null) {

            intduration= md.getDurationValue(doc);

            if (duration > 0) {
                try {

                    ArrayList<LatLng> directionPoint = md.getDirection(doc);
                    PolylineOptionsrectLine=newPolylineOptions().width(9).color(R.color.splash_blue).geodesic(true);

                    for (inti=0; i < directionPoint.size(); i++) {
                        rectLine.add(directionPoint.get(i));
                    }

                    AccessLocationActivity.this.mMap.addPolyline(rectLine);
                } catch (Exception e) {
                    e.printStackTrace();
                }

        }

UPDATED

try this

publicvoidgeoLocate(View view){

        Stringstart= start_address.getText().toString();
        Stringdestination= destination_address.getText().toString();

        Geocodergc=newGeocoder(getActivity());
        try {

            List<android.location.Address> list = gc.getFromLocationName(start,1);
            finalAddressadress1=  list.get(0);
            Stringstart_adress= adress1.getLocality();
            doublelat_start= adress1.getLatitude();
            doublelng_start= adress1.getLongitude();


            list = gc.getFromLocationName(destination,1);
            Addressadress2=  list.get(0);
            Stringdestination_adress= adress2.getLocality();
            doublelat_destination= adress2.getLatitude();
            doublelng_destination= adress2.getLongitude();

            if (start_marker != null || destination_marker != null){
                start_marker.remove();
                destination_marker.remove();
            }

            options_start = newMarkerOptions()
                    .title(start_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                    .position(newLatLng(lat_start, lng_start));
            start_marker = mGoogleMap.addMarker(options_start);
            options_destination = newMarkerOptions()
                    .title(destination_adress)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
                    .position(newLatLng(lat_destination, lng_destination));
            destination_marker = mGoogleMap.addMarker(options_destination);
            reservation.setClickable(true);
            reservation.setEnabled(true);
            reservation.setTextColor(getResources().getColor(R.color.white));
            reservation.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            if(reservation.isClickable()) {
                reservation.setOnClickListener(newView.OnClickListener() {
                    @OverridepublicvoidonClick(View v) {
                    }
                });
            }

            LatLngBounds.Builderbuilder=newLatLngBounds.Builder();
            builder.include(start_marker.getPosition());
            builder.include(destination_marker.getPosition());
            LatLngBoundsbounds= builder.build();
            finalStringBuilderurl=newStringBuilder("http://maps.googleapis.com/maps/api/directions/json?sensor=false&language=fr");
            url.append("&origin=");
            url.append(start.replace(" ", "+"));
            url.append("&destination=");
            url.append(destination.replace(" ", "+"));


            GMapV2Directionmd=newGMapV2Direction();
            Documentdoc= md.getDocument(formatAddress(start), formatAddress(destination), GMapV2Direction.MODE_DRIVING, getApplicationContext());
            if (doc != null) {

                intduration= md.getDurationValue(doc);

                if (duration > 0) {
                    try {

                        ArrayList<LatLng> directionPoint = md.getDirection(doc);
                        PolylineOptionsrectLine=newPolylineOptions().width(9).color(R.color.colorPrimary).geodesic(true);

                        for (inti=0; i < directionPoint.size(); i++) {
                            rectLine.add(directionPoint.get(i));
                        }

                        mGoogleMap.addPolyline(rectLine);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }


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

    }

Solution 2:

This is a great wrapper on top of the Google DirectionsAPI if you are willing to give it a shot.

More detailed explanation here.

Post a Comment for "Create Itinary Google Maps Android"