如何在 android 中的 google 地图上绘制动态行进路径

how to draw dynamic travel path on google map in android

i have integrated google map in android. and i am getting current location using GPS. on Onlocationchanged i am updating the current location.

My doubt is i am on the Google map UI . i wants to walk or drive that time from my current location as starting point and i wants to start draw the line on map dynamically.

i searched on net for long time but i am not getting any solution . here is my code please anyone help me to over come this problem.

private void initalizeMap()
{
     // Google Play Services are available
           // Getting GoogleMap object from the fragment
            googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

            if(gps.canGetLocation())
            {
                  double latitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LATITUDE));
                  double longtitude = Double.valueOf(Helper.loadSavedPreferences(getActivity(), NameConversion.LONGITUDE));

                  Log.d("getting gps value", ""+ latitude);
                  Log.d("getting gps Long", ""+ longtitude);
                // Creating a LatLng object for the current location
                LatLng latLng = new LatLng(latitude, longtitude);
                // create marker
                marker = new MarkerOptions().position(latLng).title("You are here!");
                // Changing marker icon
                // set yours icon here
                marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
                // Showing the current location in Google Map
                googleMap.setTrafficEnabled(true);
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                // Zoom in the Google Map
                googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));
                googleMap.addMarker(marker);
            }else{
                Toast.makeText(getActivity(), "failed To get the location", Toast.LENGTH_SHORT).show();
            }
    }

    @Override
public void onLocationChanged(Location location) {

        double latitude = gps.getLatitude();
        // Getting longitude of the current location
        double longitude = gps.getLongitude();
        // Creating a LatLng object for the current location

        Log.d("latitude map Class", "latitude:  " + gps.getLatitude());
        Log.d("longitude Map CLass", "longitude:  " + gps.getLongitude());

        LatLng latLng = new LatLng(latitude, longitude);
        // create marker
        MarkerOptions marker = new MarkerOptions().position(latLng).title("You are here!");
        // Changing marker icon
        // set yours icon here
        marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_green_point));
        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(25));

}

and i tried with following links [1 st lnikn second link

为了在 Google 地图上获得驾车或步行时间,您必须使用 Google 提供的方向 API。 API 对方向的调用将是这样的:

http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los%20Angeles,CA&sensor=false

您可以将其嵌入 class 中,您可以在其中使用 AsyncTask 进行 http 调用,如下所示:

GenericUrl url = new GenericUrl("http://maps.googleapis.com/maps/api/directions/json");
url.put("origin", origin);
url.put("destination", destination);
url.put("sensor",false);

请遵循此tutorial or this以获得方向API(包括地图上的驾车、公交、步行和骑车方向。)

希望对您有所帮助!!!